feat(streaming): stream tool call argument deltas in TemporalStreamingModel#355
Open
vkalmathscale wants to merge 3 commits into
Open
feat(streaming): stream tool call argument deltas in TemporalStreamingModel#355vkalmathscale wants to merge 3 commits into
vkalmathscale wants to merge 3 commits into
Conversation
…gModel Wire ResponseFunctionCallArgumentsDeltaEvent into the streaming layer introduced in #333, so write-heavy tools (write_file, apply_patch) no longer freeze the UI for the duration of argument generation. The model now opens a per-function-call streaming context with a ToolRequestContent placeholder, emits ToolRequestDelta updates for each argument delta, and finalizes with a StreamTaskMessageFull containing the parsed arguments on ResponseOutputItemDoneEvent. Coalescing and mode dispatch are inherited from the existing streaming infrastructure -- no new flags or surface area. ModelResponse output is unchanged; activity determinism is unaffected. End-of-loop cleanup defensively closes any function-call contexts that didn't see a Done event (truncated stream or mid-stream exception). Adds two tests covering the happy path (well-formed JSON args -> deltas + parsed Full) and the malformed-args fallback (invalid JSON -> empty dict + WARNING log).
Logging raw_args[:200] could leak partial file contents, PII, or secrets from write_file / apply_patch arguments into production log pipelines. Switch to logging only bounded metadata (tool name + raw arg byte count). The existing malformed-args test still passes since it asserts on the "Failed to parse tool call arguments" prefix, which is preserved.
danielmillerp
approved these changes
May 18, 2026
…call-arg-deltas # Conflicts: # src/agentex/lib/core/temporal/plugins/openai_agents/models/temporal_streaming_model.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
TemporalStreamingModelalready streams text deltas and reasoning summary deltas to Redis viaStreamingTaskMessageContext, butResponseFunctionCallArgumentsDeltaEventwas being silently buffered intofunction_calls_in_progress[...]['arguments']with no per-delta publish. Consumers only saw the completed tool call surface later (after the activity returned, via downstream hooks if any).For write-heavy tools —
write_file,apply_patch, anything that puts a 2–20KB string into a single argument — the model spends multiple seconds generating the argument body, and the UI sees nothing until the entire activity finishes. The result is a frozen UI followed by an abrupt jump when the activity returns.This PR threads tool-call argument deltas through the same streaming machinery used for text and reasoning, riding on the
CoalescingBuffer+StreamingModeinfrastructure added in #333. The buffer's merge helpers already key ontool_call_idforToolRequestDelta, so coalescing, mode dispatch, and opt-out are inherited from existing infra.Design
TemporalStreamingModelnow opens astreaming_task_message_contextper function call (keyed off the call'soutput_index), withinitial_content=ToolRequestContent(...)and the model's configuredstreaming_mode. Three event handlers participate:ResponseOutputItemAddedEvent(type=function_call)function_calls_in_progress[output_index]['context'].ResponseFunctionCallArgumentsDeltaEventStreamTaskMessageDelta(delta=ToolRequestDelta(arguments_delta=..., tool_call_id=..., name=...))into the per-call context. The coalescing buffer merges consecutive deltas with the sametool_call_id.ResponseOutputItemDoneEvent(type=function_call)JSONDecodeError), emit a finalStreamTaskMessageFull(content=ToolRequestContent(...)), and close the context.End-of-loop cleanup defensively closes any function-call contexts that didn't see a
Doneevent (truncated stream or mid-stream exception).ModelResponseoutput is unchanged:output_itemsstill receives the same completeResponseFunctionToolCall. Activity determinism is unaffected — streaming is a side effect.What this does NOT change
StreamingModeis already the on/off knob. No new flag.streaming_mode="off"suppresses tool-arg deltas the same way it suppresses text deltas."per_token"publishes immediately;"coalesced"(default) batches at 50ms / 128 chars.TemporalStreamingHooks.on_tool_startis unchanged. It still fires after the activity returns and still emits aToolRequestContentFull message via thestream_lifecycle_contentactivity. See Caveats.Caveats
Overlap with
TemporalStreamingHooks.on_tool_start. Users who passTemporalStreamingHookstoRunner.runwill now see two persistedtask_messages per tool call: one created by the model (delta stream + final Full) and one created by the hook (Full only). Both land on the same Redis topictask:{task_id}with differentparent_task_message.ids, so a default UI will render two cards for the same logical tool call.This needs a follow-up to decide which path owns the canonical
ToolRequestemission. Options for review discussion:on_tool_start's Full emit when the model is also emitting (auto-detect via a workflow-instance flag, mirroring how_task_id/_trace_idare threaded today).on_tool_start's Full emit entirely in a follow-up major bump (the model becomes the single source of truth forToolRequestevents).Until that follow-up, users who want streamed tool args without duplicate emits should subclass
TemporalStreamingHooksand overrideon_tool_startto a no-op.Coalescing windows still apply. With the default 50ms / 128-char window, tool args render in ~50ms-granularity chunks rather than per-token. This is the same tradeoff already made for text streaming in perf(streaming): coalesce per-token publishes to Redis (50ms / 128-char window) #333, and the right default for write-heavy tools (UX value is "watch the artifact appear", not "see each token").
Malformed argument JSON. If the model produces invalid JSON for the args (truncated stream, hallucinated structure), the path logs a WARNING and emits
arguments={}in the finalToolRequestContent. The raw delta stream is preserved on the consumer side regardless — only the structured final view falls back.Test plan
test_streaming_model.py::TestStreamingModelFunctionCallArgsStreaming:ToolRequestContent, oneStreamTaskMessageDelta(ToolRequestDelta)perArgumentsDeltaevent preserving the delta text, and one finalStreamTaskMessageFull(ToolRequestContent)with parsed args.arguments={}in the final Full and logs a WARNING.test_streaming_model.pysuite passes (42/42).ruff checkclean on both modified files.streaming_mode="off"suppresses tool-arg deltas (only the final persisted message exists on close).cc reviewers familiar with #333's
CoalescingBufferdesign.Greptile Summary
This PR extends
TemporalStreamingModelto publish tool-call argument deltas to Redis in real time, eliminating the UI freeze while the model generates write-heavy tool arguments likewrite_file. It opens a per-function-callstreaming_task_message_contextonResponseOutputItemAddedEvent, emits aToolRequestDeltaperResponseFunctionCallArgumentsDeltaEvent, and closes with a finalToolRequestContentFull message onResponseOutputItemDoneEvent, riding the sameCoalescingBuffer+StreamingModeinfrastructure introduced in #333.contextslot infunction_calls_in_progress, and a defensive post-loop cleanup.ModelResponseoutput and activity determinism are unchanged.TaskMessageto pass Pydantic validation.TemporalStreamingHookswill see duplicateToolRequesttask messages per tool call until a follow-up resolves ownership."Confidence Score: 5/5
Safe to merge; the new streaming path is a pure side effect that does not alter ModelResponse output or activity determinism.
The change threads tool-call argument deltas through the existing streaming infrastructure without touching the text or reasoning paths. The double-close concern from the prior review thread is correctly addressed via finally: call_data['context'] = None. The orphan-cleanup block only runs on normal stream exhaustion, consistent with the pre-existing pattern for streaming_context and reasoning_context. The duplicate-emit overlap with TemporalStreamingHooks.on_tool_start is explicitly documented and deferred.
No files require special attention beyond the acknowledged TemporalStreamingHooks overlap documented in the PR description.
Important Files Changed
Sequence Diagram
sequenceDiagram participant OAI as OpenAI Stream participant TSM as TemporalStreamingModel participant CTX as per-call StreamingContext participant Redis as Redis OAI->>TSM: ResponseOutputItemAddedEvent (function_call) TSM->>CTX: "open context with ToolRequestContent(arguments={})" CTX->>Redis: persist initial ToolRequestContent loop For each argument chunk OAI->>TSM: ResponseFunctionCallArgumentsDeltaEvent TSM->>CTX: stream_update StreamTaskMessageDelta(ToolRequestDelta) CTX->>Redis: publish delta coalesced at 50ms/128ch end OAI->>TSM: ResponseFunctionCallArgumentsDoneEvent Note over TSM: overwrite call_data arguments with authoritative string OAI->>TSM: ResponseOutputItemDoneEvent (function_call) TSM->>CTX: stream_update StreamTaskMessageFull(ToolRequestContent parsed args) CTX->>Redis: persist final ToolRequestContent TSM->>CTX: close() OAI->>TSM: ResponseCompletedEvent Note over TSM: output_items = response.output for ModelResponse Note over TSM: post-loop cleanup closes contexts missing Done eventPrompt To Fix All With AI
Reviews (3): Last reviewed commit: "Merge remote-tracking branch 'origin/nex..." | Re-trigger Greptile