Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 4 additions & 0 deletions nemo_gym/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,11 @@
start_model_execution,
)
from nemo_gym.models.responses_streaming import (
NamespaceMap,
flatten_namespace_tools,
is_responses_lifecycle_chunk,
restore_namespace_tool_calls,
sanitize_streaming_responses_body,
synthesize_responses_sse,
)
from nemo_gym.models.token_capture.external_capture import (
Expand All @@ -75,6 +77,8 @@
# What code outside the library, such as servers, imports from this package. Each name's submodule loads
# on first use. Code inside nemo_gym imports the submodules directly.
_PUBLIC_API = {
"NamespaceMap": "responses_streaming",
"sanitize_streaming_responses_body": "responses_streaming",
"AnthropicConverter": "anthropic_converter",
"BaseResponsesAPIModelConfig": "base_responses_api_model",
"Body": "base_responses_api_model",
Expand Down
8 changes: 8 additions & 0 deletions responses_api_models/openai_model/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@

OpenAI-compatible model server using Gym's shared HTTP client.

## Streaming harness tools

The Responses route preserves custom tools declared in `additional_tools` input items,
including Codex's `functions.exec`. Gym forwards their definitions in `tools`, restores
namespaces on returned calls, and keeps their identity when the client replays tool results.
No `function_bridge` setting is needed for a Responses backend. Explicit
`drop_custom_tools` and `drop_hosted_tools` settings still apply.

## HTTP retries

HTTP 404 and 408 are retryable by default, alongside the existing transient
Expand Down
17 changes: 16 additions & 1 deletion responses_api_models/openai_model/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,13 @@
NeMoGymResponse,
NeMoGymResponseCreateParamsNonStreaming,
)
from nemo_gym.models import BaseResponsesAPIModelConfig, Body, SimpleResponsesAPIModel
from nemo_gym.models import (
BaseResponsesAPIModelConfig,
Body,
NamespaceMap,
SimpleResponsesAPIModel,
sanitize_streaming_responses_body,
)
from nemo_gym.telemetry import MeteredSemaphore
from responses_api_models.openai_model.provider_refusals import provider_refusal, refusal_response

Expand Down Expand Up @@ -293,6 +299,15 @@ def _validate_request_overrides(self) -> "SimpleModelServerConfig":
class SimpleModelServer(SimpleResponsesAPIModel):
config: SimpleModelServerConfig

def _sanitize_streaming_responses_body(self, body: dict) -> tuple[dict, NamespaceMap]:
"""Preserve carrier custom tools for the Responses backend under the configured drop policy."""
return sanitize_streaming_responses_body(
body,
drop_custom_tools=self.config.drop_custom_tools,
drop_hosted_tools=self.config.drop_hosted_tools,
hoist_carrier_custom_tools=True,
)

def _upstream_body(self, body: BaseModel) -> Dict[str, Any]:
"""extra_body, then the caller's fields, then request_overrides; the model is always the configured one."""
body_dict = (
Expand Down
66 changes: 66 additions & 0 deletions responses_api_models/openai_model/tests/test_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,72 @@ def _setup_server(self, max_concurrent_requests=None, drop_input_reasoning_items
async def test_sanity(self) -> None:
self._setup_server()

def test_code_mode_custom_tool_and_continuation(self) -> None:
server = self._setup_server()
tool = {"type": "custom", "name": "exec", "format": {"type": "text"}}
call = {
"type": "custom_tool_call",
"id": "ctc_1",
"call_id": "call_1",
"name": "functions__exec",
"input": "await tools.apply_patch('synthetic patch')",
}
server._client = MagicMock(spec=NeMoGymAsyncOpenAI)
server._client.create_response = AsyncMock(return_value={**_response_data(), "output": [call]})
body = {
"model": "openai/openai/synthetic",
"stream": True,
"reasoning": {"effort": "xhigh"},
"input": [
{"type": "additional_tools", "tools": [{"type": "namespace", "name": "functions", "tools": [tool]}]},
{"type": "message", "role": "user", "content": "Edit a file."},
],
}
with TestClient(server.setup_webserver()) as client:
response = client.post("/v1/responses", json=body)
assert response.status_code == 200
events = [json.loads(line[6:]) for line in response.text.splitlines() if line.startswith("data: ")]
returned = events[-1]["response"]["output"][0]
assert (returned["namespace"], returned["name"], returned["input"]) == ("functions", "exec", call["input"])
forwarded = server._client.create_response.call_args.kwargs
assert forwarded["tools"] == [{**tool, "name": "functions__exec"}]
assert forwarded["reasoning"]["effort"] == "xhigh"
body["input"].extend(
[
returned,
{"type": "custom_tool_call_output", "call_id": "call_1", "output": "patched"},
]
)
assert client.post("/v1/responses", json=body).status_code == 200
replay = server._client.create_response.call_args.kwargs["input"]
assert replay[-2]["name"] == "functions__exec" and "namespace" not in replay[-2]
assert replay[-1]["output"] == "patched"

@pytest.mark.parametrize("drop_custom,drop_hosted", [(True, False), (False, True), (True, True)])
def test_code_mode_respects_explicit_tool_drop_policy(self, drop_custom: bool, drop_hosted: bool) -> None:
server = self._setup_server(drop_custom_tools=drop_custom, drop_hosted_tools=drop_hosted)
server._client = MagicMock(spec=NeMoGymAsyncOpenAI)
server._client.create_response = AsyncMock(return_value=_response_data())
custom = {"type": "custom", "name": "exec", "format": {"type": "text"}}
hosted = {"type": "web_search"}
with TestClient(server.setup_webserver()) as client:
response = client.post(
"/v1/responses",
json={
"stream": True,
"tools": [hosted],
"input": [
{"type": "additional_tools", "tools": [custom]},
{"role": "user", "content": "Answer the question."},
],
},
)
assert response.status_code == 200
forwarded = server._client.create_response.call_args.kwargs
assert forwarded.get("tools", []) == ([hosted] if not drop_hosted else []) + (
[custom] if not drop_custom else []
)

async def test_session_key_headers_reach_the_upstream_client(self):
assert self._setup_server(session_key_headers=["X-Session"])._client.session_key_headers == ["X-Session"]
assert self._setup_server()._client.session_key_headers == []
Expand Down
Loading