Repository navigation
fix(core): keep lazy Iterable fields in request and response models as lists - #4323
Merged
Merged
Conversation
|
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. |
…code The vLLM model server copies chat-completion logprobs into `output_text.logprobs`. The vendored OpenAI `Logprob` type declares `bytes` and `top_logprobs` as `Iterable`, and Pydantic validates an `Iterable` field into a lazy iterator that can be read only once. When the model writes text and a tool call in the same turn, the agent sends that assistant message back as input on the next turn. `ServerClient.request` dumps the request body in Python mode, which leaves the lazy iterator in place, so orjson fails with "Type is not JSON serializable: SerializationIterator" and the agent's `/run` returns HTTP 500. Serializing the same object twice also returns empty `bytes` and `top_logprobs` lists without any error. Gym copies of `Logprob` and `LogprobTopLogprob` now use `List`, following the existing Iterable overrides in `nemo_gym/openai_utils.py`. The encoded request bytes are unchanged. Signed-off-by: Ananth Subramaniam <[email protected]>
ananthsub
force-pushed
the
ananthsub/fix-serialization-iterator
branch
from
October 9, 2026 20:54
9429905 to
8ebe874
Compare
Contributor
The OpenAI SDK types several request fields as Iterable. Pydantic validates an Iterable into a lazy iterator that can be read only once. A request holding one cannot be encoded by orjson in ServerClient.request, cannot be deep-copied, and returns an empty list on its second serialization. After the logprobs fix, these request paths still validated lazily: - Responses tool_choice: allowed_tools.tools - Responses tools: namespace tools, file_search compound filters, shell container_auto and local skills, and container allowlist domain_secrets (shell and code_interpreter) - Chat Completions prediction.content and tool_choice allowed_tools.tools Add Gym copies of those TypedDicts with List fields, and Gym copies of the ToolParam, ToolChoice, and ChatCompletionToolChoiceOptionParam unions with the same members in the same order. Encoded bytes match the previous first mode="json" dump. Add a test that walks the core schemas of the public request and response models and fails on any generator schema, a test that pins the Gym union copies to the SDK unions, and an encode, copy, and repeated-dump test for each path. Signed-off-by: Ananth Subramaniam <[email protected]>
yaoyu-33
approved these changes
Oct 10, 2026
yaoyu-33
left a comment
Contributor
There was a problem hiding this comment.
LGTM. Eager collections preserve replay and the wire format; 887 targeted tests and parent/head serialization checks passed.
Contributor
|
🔄 Merge queue validation started! You can track the progress here: https://github.com/NVIDIA-NeMo/Gym/actions/runs/38079484503 |
This branch was previously deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What changed
No public request or response model in
nemo_gym/openai_utils.pykeeps a field that Pydantic validates into a lazy iterator. Each change follows the existing Iterable overrides in that file, such as the ones forannotations,content,summary, andtools.The first commit fixes model output that the agent sends back as input.
NeMoGymResponseOutputText.logprobsnow uses Gym copies of the OpenAILogprobandLogprobTopLogprobtypes, withListin place ofIterableforbytesandtop_logprobs.The second commit fixes request configuration. It adds Gym copies with
Listfields for these SDK types:tool_choiceallowed tools:toolstoolsfilterscontainer_autoandlocalenvironmentskillsdomain_secretsprediction.contenttool_choiceallowed tools:allowed_tools.toolsThe request models now use Gym copies of the SDK
ToolParam,ToolChoice, andChatCompletionToolChoiceOptionParamunions. Each copy keeps the SDK members in the SDK order and swaps in aListsubclass only where the SDK member has anIterablefield. Because the member order is the same, union validation picks the same member as before.NeMoGymResponseandNeMoGymChatCompletionhad no remaining lazy fields.Why the agent's next request fails
Since #3618, the vLLM model server copies chat-completion logprobs into
output_text.logprobs. The OpenAILogprobtype declaresbytesandtop_logprobsasIterable, and Pydantic validates anIterablefield into a lazy iterator that can be read only once.When the model writes text and a tool call in the same turn,
simple_agentsends that assistant message back as input on the next turn.ServerClient.requestdumps the request body withmodel_dump(exclude_unset=True)in Python mode, which leaves the lazy iterator in the output as aSerializationIterator. orjson cannot encode that object, so the post to the model server fails and the agent's/runreturns HTTP 500:This happened in step 1 of a 2-step NeMo-RL GRPO smoke run with Qwen3-0.6B, vLLM with the
hermestool parser, and the workplace_assistant environment withsimple_agent. An earlier run with the same code and data passed, because the failure only occurs when an assistant text message with logprobs is followed by another turn.The lazy type also loses data without an error. Serializing the same response object a second time returns empty
bytesandtop_logprobslists, andmodel_copy(deep=True)cannot copy the object at all because the iterator cannot be pickled.Why the fix is in the type and not in
ServerClient.requestSwitching
ServerClient.requesttomodel_dump(mode="json")would make the first encode succeed, but the dump would still read the iterator to the end. Every later turn would then send empty logprobs to the model server. Storing the values as lists keeps them complete for every serialization.Wire format
The encoded request bytes are unchanged. For a request with a user message, an assistant message with logprobs, a function call, and a function call output, the new Python-mode dump encoded by orjson is byte-identical to the previous
mode="json"encoding of the same body.Related issue
No issue is filed, because the bug was found in a smoke run and the fix is contained in one type.
Request configuration fields
The request configuration fields fail the same way when a dataset sets them. For example, this request cannot be encoded by orjson after validation:
Its
model_copy(deep=True)raisescannot pickle 'ValidatorIterator', and a secondmodel_dump(mode="json")returns an emptytoolslist in the namespace tool.Validation
tests/unit_tests/test_openai_utils.pybuilds a next-turn request the waysimple_agentdoes. It validates a model response with output text logprobs and a function call, deep-copies the request body, and appends the output and a function call output. The test checks that orjson encodes the request with complete logprobs on repeated serializations.tests/unit_tests/test_server_utils.pysends a request that replays an assistant message with logprobs throughServerClient.postand checks the encoded request data.tests/unit_tests/test_openai_utils.pywalks the Pydantic core schemas ofNeMoGymResponseCreateParamsNonStreaming,NeMoGymResponse,NeMoGymChatCompletionCreateParamsNonStreaming, andNeMoGymChatCompletion, and fails if any field path reaches ageneratorschema. The failure message names each path. This test also catches lazy fields that a future OpenAI SDK upgrade adds.mode="json"dump. The bytes were identical, including key order. Requests that leave out a required key in the new copies still fail validation.Commands:
Rollout evidence
Pending: the 2-step NeMo-RL GRPO smoke run on workplace_assistant will be repeated before this PR is marked ready for review.