Skip to navigation

nemo_gym.responses_converter

View as Markdown

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

NameDescription
ResponsesConverterConverts between OpenAI Responses API and Chat Completions API formats.
ResponsesConverterState-

Functions

NameDescription
_align_logprobs_to_textKeep the token entries that survive removing the removed character spans.
_chat_logprobs_to_responsesChat choice logprobs in the shape ResponseOutputText.logprobs expects.
_message_content_to_textPlain text of a chat message content (a string, or a list of text parts).
_optional_token_countReturn a provider-reported token count without coercing missing/invalid values to zero.
_token_information_from_mappingReturn validated token metadata when the mapping contains it.
_usage_detailRead one canonical nested token detail, then named provider aliases.
split_responses_input_output_items-

Data

VLLMConverter

VLLMConverterResponsesToChatCompletionsState

_IMAGE_OUTPUT_PLACEHOLDER

_REPORTING_ONLY_REASONING_FIELDS

_RESPONSE_NON_BOUNDARY_TYPES

_RESPONSE_OUTPUT_BOUNDARY_TYPES

logger

API

class nemo_gym.responses_converter.ResponsesConverter()

Bases: BaseModel

Converts between OpenAI Responses API and Chat Completions API formats.

THINK_TAG_PATTERN
ClassVar = re.compile('<think>(.*?)</think>', re.DOTALL)
return_token_id_information
bool
uses_reasoning_parser
bool = True
nemo_gym.responses_converter.ResponsesConverter._chat_completion_to_responses_tools(
chat_completions_tools: typing.Optional[typing.List[nemo_gym.openai_utils.NeMoGymChatCompletionToolUnionParam]]
) -> typing.List[openai.types.responses.response_create_params.ToolParam]
nemo_gym.responses_converter.ResponsesConverter._chat_custom_tool_to_responses(
tool: dict
) -> dict
staticmethod
nemo_gym.responses_converter.ResponsesConverter._chat_to_responses_tool_choice(
tool_choice: typing.Any
) -> typing.Any
nemo_gym.responses_converter.ResponsesConverter._extract_reasoning_from_content(
content: str
) -> typing.Tuple[typing.List[str], str]
nemo_gym.responses_converter.ResponsesConverter._format_function_call(
m: dict,
) -> None
nemo_gym.responses_converter.ResponsesConverter._format_function_call_output(
m: dict,
drop_unrepresentable_request_fields: bool = False
) -> None
nemo_gym.responses_converter.ResponsesConverter._format_message(
m: dict,
drop_unrepresentable_request_fields: bool = False
) -> None
nemo_gym.responses_converter.ResponsesConverter._format_reasoning(
m: dict,
) -> None

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.

nemo_gym.responses_converter.ResponsesConverter._parse_think_tags(
content: str
) -> typing.Tuple[typing.List[str], str]
classmethod
nemo_gym.responses_converter.ResponsesConverter._responses_custom_tool_to_chat(
tool: dict
) -> dict
staticmethod
nemo_gym.responses_converter.ResponsesConverter._responses_to_chat_tool_choice(
tool_choice: typing.Any
) -> typing.Any
nemo_gym.responses_converter.ResponsesConverter._tool_reference_to_chat(
tool: dict
) -> dict
staticmethod
nemo_gym.responses_converter.ResponsesConverter._tool_reference_to_responses(
tool: dict
) -> dict
staticmethod
nemo_gym.responses_converter.ResponsesConverter._wrap_reasoning_in_think_tags(
texts: typing.List[str]
) -> str
staticmethod
nemo_gym.responses_converter.ResponsesConverter.chat_completion_to_response(
preserve_envelope_id: bool = False
nemo_gym.responses_converter.ResponsesConverter.chat_completion_to_responses_create_params(
nemo_gym.responses_converter.ResponsesConverter.chat_completions_messages_to_responses_items(
messages: typing.List[typing.Dict[str, typing.Any]]
nemo_gym.responses_converter.ResponsesConverter.postprocess_assistant_message_dict(
message_dict: typing.Dict[str, typing.Any],
logprobs: typing.Any = None
nemo_gym.responses_converter.ResponsesConverter.postprocess_chat_response(
nemo_gym.responses_converter.ResponsesConverter.responses_to_chat_completion_create_params(
drop_unrepresentable_request_fields: bool = False

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, 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. Every other unrepresentable field, reasoning.context included, is still refused.

class nemo_gym.responses_converter.ResponsesConverterState()

Bases: BaseModel

assistant_item_buffered
bool = False
content_buffer
str = ''
messages
List[NeMoGymChatCompletionMessageParam] = Field(default_factory=list)
refusal_buffer
str = ''
return_token_id_information
bool
token_information
Optional[TokenIDLogProbMixin] = None
tool_calls_buffer
List[NeMoGymChatCompletionMessageToolCallParam] = Field(default_factory=list)
nemo_gym.responses_converter.ResponsesConverterState.flush_assistant() -> None
nemo_gym.responses_converter._align_logprobs_to_text(
logprobs: typing.Optional[typing.List[typing.Dict[str, typing.Any]]],
original: str,
removed: typing.List[typing.Tuple[int, int]]
) -> typing.Optional[typing.List[typing.Dict[str, typing.Any]]]

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.

nemo_gym.responses_converter._chat_logprobs_to_responses(
logprobs: typing.Any
) -> typing.Optional[typing.List[typing.Dict[str, typing.Any]]]

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.

nemo_gym.responses_converter._message_content_to_text(
content: typing.Any
) -> str

Plain text of a chat message content (a string, or a list of text parts).

nemo_gym.responses_converter._optional_token_count(
value: typing.Any
) -> typing.Optional[int]

Return a provider-reported token count without coercing missing/invalid values to zero.

nemo_gym.responses_converter._token_information_from_mapping(
value: typing.Dict[str, typing.Any]

Return validated token metadata when the mapping contains it.

nemo_gym.responses_converter._usage_detail(
usage: typing.Any,
detail_group: str,
detail_name: str,
top_level_aliases: str = ()
) -> typing.Optional[int]

Read one canonical nested token detail, then named provider aliases.

nemo_gym.responses_converter.split_responses_input_output_items(
nemo_gym.responses_converter.VLLMConverter = ResponsesConverter
nemo_gym.responses_converter.VLLMConverterResponsesToChatCompletionsState = ResponsesConverterState
nemo_gym.responses_converter._IMAGE_OUTPUT_PLACEHOLDER = '[image omitted from tool output]'
nemo_gym.responses_converter._REPORTING_ONLY_REASONING_FIELDS = ('summary', 'generate_summary')
nemo_gym.responses_converter._RESPONSE_NON_BOUNDARY_TYPES: frozenset[str] = frozenset({'additional_tools', 'apply_patch_call_output', 'compaction', 'compact...
nemo_gym.responses_converter._RESPONSE_OUTPUT_BOUNDARY_TYPES = frozenset({'code_interpreter_call', 'computer_call', 'custom_tool_call', 'file_s...
nemo_gym.responses_converter.logger = logging.getLogger(__name__)