Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion nemo_gym/openai_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -931,12 +931,22 @@ class NeMoGymChatCompletionMessageToolCallFunctionParam(TypedDict, total=False):
name: Required[str]


# `NeMoGymChatCompletionCreateParamsNonStreaming` sets `extra="forbid"`, and
# pydantic propagates that config into every nested `TypedDict`. Tool calls are
# echoed back by agents from whatever the provider returned, so they routinely
# carry provider-specific keys that are not in the OpenAI schema. Opt the tool
# call shapes out so those keys are dropped rather than rejected; the request
# model itself stays strict.
_TOOL_CALL_CONFIG = ConfigDict(extra="ignore")
Comment thread
ananthsub marked this conversation as resolved.
Resolved


class NeMoGymChatCompletionMessageToolCallParam(ChatCompletionMessageToolCallParam):
__pydantic_config__ = _TOOL_CALL_CONFIG
function: NeMoGymChatCompletionMessageToolCallFunctionParam


class NeMoGymChatCompletionMessageCustomToolCallParam(ChatCompletionMessageCustomToolCallParam):
pass
__pydantic_config__ = _TOOL_CALL_CONFIG


NeMoGymChatCompletionMessageToolCallUnionParam = Annotated[
Expand Down
72 changes: 72 additions & 0 deletions tests/unit_tests/test_openai_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,78 @@ def test_custom_tool_and_training_tool_call_round_trip(self) -> None:
assert params.messages[0]["tool_calls"][0]["type"] == "custom"
assert params.messages[0]["generation_token_ids"] == [2]

def test_tool_call_keeps_unknown_keys_out_of_the_way(self) -> None:
"""Agents echo tool calls back verbatim, so they carry provider-specific keys.

The request model is `extra="forbid"` and pydantic propagates that into
nested TypedDicts, which would otherwise turn a harmless extra key into a
hard validation error for every tool-calling agent.
"""
payload = {
"messages": [
{
"role": "assistant",
"content": "",
"tool_calls": [
{
"id": "call-1",
"name": "KB_search",
"function": {
"name": "KB_search",
"arguments": '{"query": "hello"}',
},
"type": "function",
}
],
}
],
"model": "gpt-test",
}

params = NeMoGymChatCompletionCreateParamsNonStreaming.model_validate(payload)

tool_call = params.messages[0]["tool_calls"][0]
assert tool_call["type"] == "function"
assert tool_call["function"] == {"name": "KB_search", "arguments": '{"query": "hello"}'}
assert "name" not in tool_call

def test_custom_tool_call_keeps_unknown_keys_out_of_the_way(self) -> None:
payload = {
"messages": [
{
"role": "assistant",
"content": "",
"tool_calls": [
{
"id": "call-1",
"type": "custom",
"custom": {"name": "shell", "input": "echo hello"},
"name": "shell",
}
],
}
],
"model": "gpt-test",
}

params = NeMoGymChatCompletionCreateParamsNonStreaming.model_validate(payload)

tool_call = params.messages[0]["tool_calls"][0]
assert tool_call["type"] == "custom"
assert tool_call["custom"] == {"name": "shell", "input": "echo hello"}
assert "name" not in tool_call

def test_unknown_top_level_request_key_is_still_rejected(self) -> None:
"""Tolerating extras on tool calls must not loosen the request model."""
with pytest.raises(ValidationError):
NeMoGymChatCompletionCreateParamsNonStreaming.model_validate(
{
"messages": [{"role": "user", "content": "hi"}],
"model": "gpt-test",
"not_a_real_request_field": True,
}
)

def test_custom_response_tool_call_round_trip(self) -> None:
payload = {
"id": "chatcmpl-1",
Expand Down
Loading