Skip to content

vllm.entrypoints.openai.responses.streaming_events

Streaming SSE event builders for the Responses API.

Pure functions that translate streaming state + delta data into OpenAI Response API SSE events. Used by the streaming event processors in serving.py.

The file is organized as
  1. StreamingState dataclass + utility helpers
  2. Shared leaf helpers — delta events (take plain strings, no context)
  3. Shared leaf helpers — done events (take plain strings, no context)
  4. Harmony-specific dispatchers (route ctx/previous_item → leaf helpers)
  5. Harmony-specific tool lifecycle helpers

Classes:

Functions:

SimpleStreamingEventProcessor

State-machine processor for the simple (non-Harmony) streaming path.

Core flow
  1. Resolve the target state from the delta_message (CONTENT / REASONING / TOOL_CALL).
  2. If the target state differs from the current one, close_current() then open() the new state.
  3. emit_delta() produces the incremental events for the state.
State lifecycle

open() -> repeated emit_delta() -> close_current()

Methods:

  • close_current –

    Close the current state and emit its 'done' event sequence.

  • emit_delta –

    Emit incremental events for the current state from the delta.

  • needs_transition –

    Return True when we must close the current state and open a new one.

  • open –

    Open a new state and emit its 'added' / 'open' event sequence.

  • resolve_target_state –

    Decide which state the next delta belongs to.

close_current(status='completed')

Close the current state and emit its 'done' event sequence.

emit_delta(delta_message, output, get_logprobs=None)

Emit incremental events for the current state from the delta.

needs_transition(target_state, tool_call)

Return True when we must close the current state and open a new one.

Two cases trigger a transition
  1. The target state differs from the current state (e.g. CONTENT -> TOOL_CALL).
  2. We are already in TOOL_CALL but the next tool_call has a different index (multiple consecutive tool calls).

open(target_state, tool_call=None)

Open a new state and emit its 'added' / 'open' event sequence.

resolve_target_state(delta_message)

Decide which state the next delta belongs to.

Priority: TOOL_CALL > REASONING > CONTENT, fallback to NONE. For TOOL_CALL the first tool_call object is also returned so callers can detect a switch between consecutive tools.

StreamingState dataclass

Mutable state for streaming event processing.

Methods:

reset_for_new_item()

Reset state when expecting a new output item.

_StateHandlers

Bases: NamedTuple

Tuple for each state: open(start), delta(chunk), done(finish).

_resolve_mcp_name_label(recipient)

Resolve MCP tool name and server label from a recipient string.

  • mcp.* recipients: strip prefix, use the bare name as both name and server_label.
  • Everything else: use the recipient as the name and look up the server_label in TOOL_NAME_TO_MCP_SERVER_LABEL.

emit_browser_tool_events(previous_item, state)

Emit events for browser tool calls (web search).

emit_code_interpreter_completion_events(previous_item, state)

Emit events when code interpreter completes.

emit_code_interpreter_delta_events(delta, state)

Emit events for code interpreter delta streaming.

emit_content_delta_events(segment, state, function_tool_names=None, logprobs=None)

Emit events for content delta streaming based on channel type.

This is a Harmony-specific dispatcher that extracts values from the latest append segment and delegates to shared leaf helpers.

emit_function_call_delta_events(delta, function_name, state)

Emit events for function call argument deltas.

emit_function_call_done_events(function_name, arguments, state)

Emit events when a function call completes.

emit_mcp_completion_events(recipient, arguments, state)

Emit events when an MCP tool call completes.

emit_mcp_delta_events(delta, state, recipient)

Emit events for MCP tool delta streaming.

emit_previous_item_done_events(previous_item, state, function_tool_names=None, logprobs=None)

Emit done events for the previous item when expecting a new start.

This is a Harmony-specific dispatcher that extracts values from the Harmony parser's message object and delegates to shared leaf helpers.

emit_reasoning_delta_events(delta, state)

Emit events for reasoning text delta streaming.

emit_reasoning_done_events(text, state)

Emit events when a reasoning (analysis) item completes.

emit_text_delta_events(delta, state, logprobs=None)

Emit events for text content delta streaming.

emit_text_output_done_events(text, state, logprobs=None)

Emit events when a final text output item completes.

emit_tool_action_events(previous_item, state, tool_server)

Emit events for a completed assistant action turn.

is_mcp_tool_by_namespace(recipient, allowed_function_tool_names=None)

Determine if a tool call is an MCP tool based on recipient prefix.

Inverse of is_function_recipient — everything that is not a function call is an MCP tool.

split_delta(delta)

Decompose a DeltaMessage with multiple fields into atomic deltas.

The Responses API emits typed SSE events (one type per event), so a compound DeltaMessage must be split before entering the state machine. Order: reasoning -> content -> tool_calls (grouped by index).