nemo_gym.models.chat_completions_converter
nemo_gym.models.chat_completions_converter
Shared Responses API ↔ Chat Completions converter.
This module contains the translation logic between OpenAI’s Responses API format and the Chat Completions API format. It is used by model servers that need to convert between the two formats (e.g. vllm_model, inference_provider).
Module Contents
Classes
Functions
Data
VLLMConverterResponsesToChatCompletionsState
_REPORTING_ONLY_REASONING_FIELDS
_RESPONSE_OUTPUT_BOUNDARY_TYPES
API
Bases: BaseModel
Converts between OpenAI Responses API and Chat Completions API formats.
A Chat tool message’s text or image part as the matching function_call_output part.
The function-tool lowering of a Responses custom tool definition.
The free-form payload becomes the one required string parameter.
description is optional on a custom tool and is carried only when
present. A grammar format is dropped, with a warning once per tool
name: no vLLM tool parser enforces a grammar through function calling,
so the constraint falls back to the model’s own behavior, the same as a
hosted model that ignores the grammar.
Recover the free-form input from bridged function arguments.
Malformed arguments, not JSON or not the expected one-parameter object, return the raw string: the model’s payload is better delivered unwrapped than lost, and the harness then sees exactly what was generated. Every malformed shape, and any dropped extra key, is logged with the tool name and call id, because the harness then runs something other than a clean bridged payload while the rollout record looks like a normal call.
Encode a custom tool call’s free-form input as bridged function arguments.
Replay a custom tool call in its function-bridged form.
The bridged arguments re-encode the item’s free-form input as the
one-parameter object rather than echoing the byte string the engine
produced. vLLM parses a replayed call’s arguments into an object before
rendering the chat template, so a well-formed call re-renders to the
same parsed object even when the byte encodings differ. A malformed
call is the limit: its raw-text lift re-encodes here as
{"input": "<raw text>"}, a different object from the one the model
generated.
Collects text from ‘reasoning’ messages in responses api and appends it to a buffer.
This is done to group together one (or multiple) reasoning message(s) into a single, cohesive block, later prepending it to a subsequent assistant message. See: https://docs.nvidia.com/nemo/gym/main/infrastructure/engineering-notes/responses-api-evolution for background on reasoning in the Responses API.
What the chat tool message carries for an input_image part of a tool call output.
A Responses tool reference in Chat form, following the bridge’s lowering.
Under function_bridge the request declares a referenced custom tool
as a function tool, and vLLM’s named tool choice admits type: "function" only, so the reference is rewritten to the function form.
Lift assistant content without interpreting tags inside a structured final answer.
Lift a chat choice; structured output permits only leading reasoning blocks.
Convert Responses create params into Chat Completions create params.
A Responses field with no Chat Completions representation raises NotImplementedError,
so the caller can route the request to a server that serves Responses natively. With
drop_unrepresentable_request_fields the reporting-only fields are removed from the
request instead and the conversion proceeds: a non-empty include (every value; an
empty one is already treated as absent), reasoning.summary and
reasoning.generate_summary, a reasoning.context of auto or all_turns (see
_DROPPABLE_REASONING_CONTEXTS), and a message phase. An image part in a tool’s output
becomes the text placeholder _IMAGE_OUTPUT_PLACEHOLDER, which does replace that part
of the prompt, unless inline_tool_output_images keeps it as an image. Every other
unrepresentable field, a current_turn reasoning context included, is still refused.
Bases: BaseModel
Keep the token entries that survive removing the removed character spans.
Reasoning extraction shortens the output text; its logprobs must shrink with it or
they describe different text. The kept positions come from the removal itself, never
from searching for the answer text, which may also occur inside the reasoning. If the
tokens do not reconstruct original the alignment is unknowable and the logprobs
are dropped rather than misattributed.
Chat choice logprobs in the shape ResponseOutputText.logprobs expects.
Same field names on both sides; the Responses models require bytes to be
iterable where chat allows null.
Return a provider-reported token count without coercing missing/invalid values to zero.
Return validated token metadata when the mapping contains it.
Read one canonical nested token detail, then named provider aliases.
Convert one Chat Completions usage block to Responses usage; None when the provider reported none.
Agents that make several chat calls per rollout sum these with accumulate_response_usage.
Plain text of a chat message content (a string, or a list of text parts).