Skip to content

vllm.entrypoints.openai.responses.context

Classes:

HarmonyContext

Bases: ConversationContext

Methods:

_attach_logprobs(segments, token_ids, logprobs)

Attach logprobs to output text segments, in place.

A token is output text if it was parsed on the final or commentary channel with no recipient (the rule used to build output_text items) and is not a Harmony control token. Tokens with an empty delta (e.g. part of a multi-byte character) still count.

_update_decode_token_usage(output)

Update token usage statistics for the decode phase of generation.

The decode phase processes the generated output tokens. This method: 1. Counts output tokens from all completion outputs 2. Updates the total output token count 3. Tracks tokens generated in the current turn

In streaming mode, this is called for each token generated. In non-streaming mode, this is called once with all output tokens.

Parameters:

  • output

    (RequestOutput) –

    The RequestOutput containing generated token information

Returns:

  • int ( int ) –

    Number of output tokens processed in this call

_update_prefill_token_usage(output)

Update token usage statistics for the prefill phase of generation.

The prefill phase processes the input prompt tokens. This method: 1. Counts the prompt tokens for this turn 2. Calculates tool output tokens for multi-turn conversations 3. Updates cached token counts 4. Tracks state for next turn calculations

Tool output tokens are calculated as: current_prompt_tokens - last_turn_prompt_tokens - last_turn_output_tokens This represents tokens added between turns (typically tool responses).

Parameters:

  • output

    (RequestOutput) –

    The RequestOutput containing prompt token information

call_container_tool(tool_session, last_msg) async

Call container tool. Expect this to be run in a stateful docker with command line terminal. The official container tool would at least expect the following format: - for tool name: exec - args: { "cmd":List[str] "command to execute", "workdir":optional[str] "current working directory", "env":optional[object/dict] "environment variables", "session_name":optional[str] "session name", "timeout":optional[int] "timeout in seconds", "user":optional[str] "user name", }

cleanup_session(*args, **kwargs) async

Can be used as coro to used in aexit.

ParsableContext

Bases: ConversationContext

Methods:

call_container_tool(tool_session, last_msg) async

Call container tool. Expect this to be run in a stateful docker with command line terminal. The official container tool would at least expect the following format: - for tool name: exec - args: { "cmd":List[str] "command to execute", "workdir":optional[str] "current working directory", "env":optional[object/dict] "environment variables", "session_name":optional[str] "session name", "timeout":optional[int] "timeout in seconds", "user":optional[str] "user name", }

cleanup_session(*args, **kwargs) async

Can be used as coro to used in aexit.

need_builtin_tool_call()

Return true if the last message is a builtin tool call that the request has enabled.

SimpleContext

Bases: ConversationContext

This is a context that cannot handle MCP tool calls.

Attributes:

final_output property

Return the final output, with complete text/token_ids/logprobs.

output_messages property

Return consolidated output as a single message.

In streaming mode, text and tokens are accumulated across many deltas. This property returns them as a single entry rather than one per delta.

TurnMetrics

Tracks token and toolcall details for a single conversation turn.

Methods:

  • copy –

    Create a copy of this turn's token counts.

  • reset –

    Reset counters for a new turn.

copy()

Create a copy of this turn's token counts.

reset()

Reset counters for a new turn.

_create_json_parse_error_messages(last_msg, e)

Creates an error message when json parse failed.