Skip to content

fix(responses): preserve structured function outputs - #2490

Merged
ananthsub merged 1 commit into
mainfrom
ananthsub/fix-responses-function-output
Aug 12, 2026
Merged

ananthsub merged 1 commit into
mainfrom
ananthsub/fix-responses-function-output

Conversation

@ananthsub

@ananthsub ananthsub commented Aug 11, 2026 •

Copy link
Copy Markdown
Contributor

Summary

The OpenAI SDK allows function_call_output.output to be either a string or a list of text, image, or file parts. Gym narrows this field to str, so structured outputs fail validation before conversion.

Gym now accepts the SDK output type. Responses input_text parts map to Chat tool-message text parts and map back without losing their structure. Chat tool messages cannot represent image or file output parts, so Responses-to-Chat conversion raises NotImplementedError and identifies the unsupported part type instead of discarding it.

flowchart TD
    OUTPUT["Responses function_call_output"] --> KIND{"Output shape"}
    KIND -->|"String"| CHATSTR["Chat tool-message string"]
    KIND -->|"input_text list"| CHATTEXT["Chat text parts"]
    KIND -->|"input_image or input_file"| ERROR["NotImplementedError"]
    CHATTEXT --> ROUNDTRIP["Responses input_text parts"]
Loading

@copy-pr-bot

copy-pr-bot Bot commented Aug 11, 2026

Copy link
Copy Markdown

Auto-sync is disabled for draft pull requests in this repository. Workflows must be run manually.

Contributors can view more details about this message here.

@ananthsub
ananthsub force-pushed the ananthsub/fix-responses-function-output branch from b342ac5 to ae7bec9 Compare August 11, 2026 22:15
@ananthsub
ananthsub marked this pull request as ready for review August 11, 2026 22:51
bxyu-nvidia
bxyu-nvidia previously approved these changes Aug 11, 2026
Accept structured `function_call_output` content allowed by the SDK.
Preserve text parts and reject image or file parts during Chat conversion.

Signed-off-by: Ananth Subramaniam <[email protected]>
@ananthsub
ananthsub force-pushed the ananthsub/fix-responses-function-output branch from ae7bec9 to 8c5018b Compare August 12, 2026 05:23
@ananthsub
ananthsub requested a review from a team as a code owner August 12, 2026 05:23
@ananthsub
ananthsub merged commit cc5d668 into main Aug 12, 2026
35 of 38 checks passed
@ananthsub
ananthsub deleted the ananthsub/fix-responses-function-output branch August 12, 2026 05:33
aroshanghias-nvd pushed a commit to aroshanghias-nvd/Gym that referenced this pull request Oct 16, 2026
## What does this PR do?

`/v1/chat/completions` now accepts image parts in a tool message,
besides text. Before, a tool message's `content` was validated as text
only, following the OpenAI SDK type. Every request that carried an image
in a tool result failed with 422 before it reached the backend.

**Why.** An agent harness may return the image a vision tool loaded
inside that tool's result, so that a vision-capable main model sees it
in its own context. [Hermes
Agent](https://github.com/NousResearch/hermes-agent) does this with
`vision_analyze` when the main model is declared vision-capable and no
auxiliary vision model is configured (`tools/vision_tools.py`, its
native path). Through Gym's model server, every such request failed with
422. Hermes then retried without the image (`agent/turn_recovery.py`),
so the model never saw it.

**Backends accept it.**
- vLLM's OpenAI-compatible server parses a tool message's content parts
like any other message's and collects the image for the model. Rendering
it is left to the chat template
(`vllm/renderers/chat_utils/chat_message.py`,
`parse_chat_message_content`).
- Sent directly, bypassing Gym, four OpenAI-compatible endpoints serving
vision models answered a tool message whose content was a text part and
an image part. Each named the image's color correctly.

**Changes.**
- `NeMoGymChatCompletionToolMessageParam.content` takes text and image
parts (`NeMoGymChatCompletionContentPartImageParam`). Other media parts
(audio, video, file) are still refused in a tool message.
- `ResponsesConverter.chat_completions_messages_to_responses_items` maps
a tool message's image part to a `function_call_output` `input_image`
part, with the image's `detail` or `auto`. Before, it read
`part["text"]` from every part and raised `KeyError` on an image part.

**Not changed.** Converting a Responses `function_call_output` image to
Chat Completions still refuses it, or replaces it with the placeholder
under `drop_unrepresentable_request_fields` (NVIDIA-NeMo#2490, NVIDIA-NeMo#3976). Mapping it
to an image part in the tool message is now possible. It would change
what a Responses client such as Codex sends to a vLLM chat backend, so
it is left for a separate change if wanted.

No issue was filed: the change is small, and the failure and the fix are
shown here end to end. Happy to open one if you prefer to discuss it
there first.

Proposed labels (I cannot apply labels): `feature`, `area:model`.

## Validation

All runs were against the final diff on `main` 06f5f89.

- New tests, each seen failing before the change:
- `tests/unit_tests/test_openai_utils.py`:
`test_tool_message_accepts_image_parts`, and
`test_tool_message_refuses_other_media_parts` (audio, video). The latter
passes before and after; it pins the scope.
-
`tests/unit_tests/test_responses_converter.py::test_chat_tool_image_to_responses_becomes_input_image`.
It failed with `KeyError: 'text'` before the change.
-
`responses_api_models/vllm_model/tests/test_app.py::TestVLLMConverter::test_tool_message_image_reaches_the_backend[False|True]`.
It failed with 422 before the change; it now checks that the backend
receives the tool message unchanged, on the plain and the streaming
path.
- `pytest tests/unit_tests/test_openai_utils.py
tests/unit_tests/test_responses_converter.py
responses_api_models/vllm_model/tests/test_app.py`: 775 passed.
- `./scripts/ci/core_unit_tests.sh`: 8,637 passed, 28 skipped.
- `gym env test +entrypoint=responses_api_models/vllm_model
+should_validate_data=true`: 536 passed.
- `pre-commit run --all-files`: passed (the guardwords check skipped:
its private catalog is unavailable outside NVIDIA's network; I reviewed
the diff for internal content by hand).

## Rollouts

The rollouts used Hermes Agent (`0e427d78`) with `model.supports_vision:
true` and no `auxiliary.vision` backend, through `vllm_model` against an
OpenAI-compatible endpoint serving a vision-language model. Each task
asks the agent to read a five-digit number from a PNG in its workspace.

- **Without the change:** every request with `vision_analyze`'s image in
the tool message got 422. Hermes retried without the image, and the
agent could not read the number.
- **With the change** (applied to `79dee227`, where the tool-message
type does not yet have `name`):
- every request was answered (200), with the image in the tool message;
- the model read the number from it in both tasks ("8-1-4-4-3",
"14535");
  - its answers were right, and the file it wrote held the number.

## Compatibility and benchmark impact

- **Requests:** the change only widens what the endpoint accepts, so a
request that validated before validates the same way.
- **Breaking risk:** code that reads a tool message's parts and assumes
text may meet an image part. In this repository, that was only the
converter fixed here; `token_id_capture` fingerprints serialize list
content as JSON.
- **Behaviour:** a backend or chat template that cannot render an image
in a tool message now receives one and may refuse it, where Gym refused
it before.
- **Benchmarks:** none of the benchmarks in this repository changes.

## Checklist

- [x] I have read the [contributing
guidelines](https://github.com/NVIDIA-NeMo/Gym/blob/main/CONTRIBUTING.md).
- [x] The change is focused; unrelated "drive-by" edits are tracked as
separate issues/PRs.
- [x] Tests added or updated and pass locally, or N/A for docs-only /
non-code changes (so CI unit/server checks pass when applicable).
- [x] Documentation added or updated, or N/A with justification. N/A: no
page documents which content parts a chat tool message accepts; the
type's comment records it.
- [x] Pre-commit checks pass locally (`pre-commit run --all-files`) (so
CI lint/format checks pass).
- [x] New source files include the required Apache-2.0 SPDX header. N/A:
no new files.
- [x] All commits have DCO sign-off (`git commit -s`) (so the DCO check
passes).

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Signed-off-by: Patrick Plattner <[email protected]>
Co-authored-by: Claude Opus 5.5 <[email protected]>

This branch was previously deployed

1 inactive deployment
public — 8c5018b6 Deployed Aug 12, 2026 by copy-pr-bot[bot] via release / finalize / notify #1345
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants