Stream provisional text from thread/sample (#65167)

## Why

Clients had to wait for the entire sample before they could display any
assistant text.

## What changed

Set `stream: true` to receive `thread/sample/textDelta` notifications
while a sample is in progress. Each notification goes to the requesting
connection and carries the original JSON-RPC `requestId` and a text
`delta`. The stream excludes commentary and reasoning.

The final RPC response still provides the complete sample and token
usage. Discard provisional text if the request fails or the final
`text` is null. Omitting `stream` or setting it to `false` returns
only the final response.

## Testing

Integration tests cover deltas arriving before completion, text supplied
only when an output item completes, and custom tool calls producing a
null sample even after provisional text has been streamed.

GitOrigin-RevId: b2ad960420ce7789722e03f9f283781030cf61fb
This commit is contained in:
Dylan Hurd
2026-12-02 04:56:45 +00:00
committed by copyberry
parent fad661242b
commit 350d8a47c4
24 changed files with 469 additions and 41 deletions
@@ -77,6 +77,7 @@ from .v2_all import ThreadRealtimeStartedNotification
from .v2_all import ThreadRealtimeTranscriptDeltaNotification
from .v2_all import ThreadRealtimeTranscriptDoneNotification
from .v2_all import ThreadRevertedNotification
from .v2_all import ThreadSampleTextDeltaNotification
from .v2_all import ThreadSearchPartialNotification
from .v2_all import ThreadSectionChangedNotification
from .v2_all import ThreadSettingsUpdatedNotification
@@ -164,6 +165,7 @@ KnownNotificationPayload: TypeAlias = (
| ThreadRealtimeTranscriptDeltaNotification
| ThreadRealtimeTranscriptDoneNotification
| ThreadRevertedNotification
| ThreadSampleTextDeltaNotification
| ThreadSearchPartialNotification
| ThreadSectionChangedNotification
| ThreadSettingsUpdatedNotification
@@ -254,6 +256,7 @@ NOTIFICATION_MODELS: dict[str, type[KnownNotificationPayload]] = {
"thread/realtime/transcript/delta": ThreadRealtimeTranscriptDeltaNotification,
"thread/realtime/transcript/done": ThreadRealtimeTranscriptDoneNotification,
"thread/reverted": ThreadRevertedNotification,
"thread/sample/textDelta": ThreadSampleTextDeltaNotification,
"thread/search/partial": ThreadSearchPartialNotification,
"thread/section/changed": ThreadSectionChangedNotification,
"thread/settings/updated": ThreadSettingsUpdatedNotification,
@@ -6802,6 +6802,25 @@ class ThreadRevertedNotification(BaseModel):
thread_id: Annotated[str, Field(alias="threadId")]
class ThreadSampleTextDeltaNotification(BaseModel):
model_config = ConfigDict(
populate_by_name=True,
)
delta: Annotated[
str,
Field(
description="Provisional text. The final RPC response determines whether the sample is usable."
),
]
request_id: Annotated[
RequestId,
Field(
alias="requestId",
description="The ID of the `thread/sample` request on this connection.",
),
]
class ThreadSearchSortKey(Enum):
created_at = "created_at"
updated_at = "updated_at"
@@ -10535,6 +10554,23 @@ class ThreadProjectUpdatedServerNotification(BaseModel):
params: ThreadProjectUpdatedNotification
class ThreadSampleTextDeltaServerNotification(BaseModel):
model_config = ConfigDict(
populate_by_name=True,
)
emitted_at_ms: Annotated[
int | None,
Field(
alias="emittedAtMs",
description="Unix timestamp (in milliseconds) when app-server emitted this notification.",
),
] = None
method: Annotated[
Literal["thread/sample/textDelta"], Field(title="Thread/sample/textDeltaNotificationMethod")
]
params: ThreadSampleTextDeltaNotification
class HookStartedServerNotification(BaseModel):
model_config = ConfigDict(
populate_by_name=True,
@@ -14608,6 +14644,7 @@ class ServerNotification(
| ThreadEnvironmentDisconnectedServerNotification
| ThreadSettingsUpdatedServerNotification
| ThreadTokenUsageUpdatedServerNotification
| ThreadSampleTextDeltaServerNotification
| TurnStartedServerNotification
| HookStartedServerNotification
| TurnCompletedServerNotification
@@ -14698,6 +14735,7 @@ class ServerNotification(
| ThreadEnvironmentDisconnectedServerNotification
| ThreadSettingsUpdatedServerNotification
| ThreadTokenUsageUpdatedServerNotification
| ThreadSampleTextDeltaServerNotification
| TurnStartedServerNotification
| HookStartedServerNotification
| TurnCompletedServerNotification