Skip to content

Your lightweight open-source personal agent.

fix(providers): preserve tool optionality in Responses requests

chengyongru932b06f0dec2
diff --git a/nanobot/providers/openai_responses/converters.py b/nanobot/providers/openai_responses/converters.py
index 608a133e3..0f3091bd0 100644
--- a/nanobot/providers/openai_responses/converters.py
+++ b/nanobot/providers/openai_responses/converters.py
@@ -183,11 +183,14 @@ def convert_tools(tools: list[dict[str, Any]]) -> list[dict[str, Any]]:
if not name:
continue
params: object = fn.get("parameters") or {}
+ strict = fn.get("strict")
converted.append({
"type": "function",
"name": name,
"description": fn.get("description") or "",
"parameters": params if isinstance(params, dict) else {},
+ # Responses may make optional fields required when strict is omitted.
+ "strict": strict if isinstance(strict, bool) else False,
})
return converted
diff --git a/tests/providers/test_azure_openai_provider.py b/tests/providers/test_azure_openai_provider.py
index 5978f0270..4e872ddee 100644
--- a/tests/providers/test_azure_openai_provider.py
+++ b/tests/providers/test_azure_openai_provider.py
@@ -279,7 +279,7 @@ def test_build_body_with_tools():
body = provider._build_body(
[{"role": "user", "content": "weather?"}], tools, None, 4096, 0.7, None, None,
)
- assert body["tools"] == [{"type": "function", "name": "get_weather", "description": "", "parameters": {}}]
+ assert body["tools"] == [{"type": "function", "name": "get_weather", "description": "", "parameters": {}, "strict": False}]
assert body["tool_choice"] == "auto"
diff --git a/tests/providers/test_extra_body_config.py b/tests/providers/test_extra_body_config.py
index e1f263740..3ea952f99 100644
--- a/tests/providers/test_extra_body_config.py
+++ b/tests/providers/test_extra_body_config.py
@@ -261,6 +261,7 @@ class TestBuildResponsesBodyExtraBody:
"name": "read_file",
"description": "Read a file",
"parameters": {"type": "object"},
+ "strict": False,
},
{"type": "web_search"},
]
@@ -306,6 +307,7 @@ class TestBuildResponsesBodyExtraBody:
"name": "read_file",
"description": "Read a file",
"parameters": {"type": "object"},
+ "strict": False,
},
{"type": "web_search"},
]
@@ -369,6 +371,7 @@ class TestBuildResponsesBodyExtraBody:
"name": "web_search",
"description": "Search with nanobot's configured backend",
"parameters": {"type": "object"},
+ "strict": False,
}]
def test_responses_extra_body_merges_include_without_duplicates(self) -> None:
diff --git a/tests/providers/test_litellm_kwargs.py b/tests/providers/test_litellm_kwargs.py
index febf06522..1c172e45d 100644
--- a/tests/providers/test_litellm_kwargs.py
+++ b/tests/providers/test_litellm_kwargs.py
@@ -1948,3 +1948,30 @@ def test_deepseek_no_backfill_when_reasoning_effort_none_string() -> None:
)
assistant = kw["messages"][1]
assert "reasoning_content" not in assistant
+
+
+async def test_responses_request_preserves_optional_tool_fields() -> None:
+ mock_responses = AsyncMock(return_value=_fake_responses_response())
+ parameters = {
+ "type": "object",
+ "properties": {"query": {"type": "string"}},
+ "required": [],
+ }
+ with patch("nanobot.providers.openai_compat_provider.AsyncOpenAI") as mock_client:
+ mock_client.return_value.responses.create = mock_responses
+ provider = OpenAICompatProvider(
+ api_key="sk-test", default_model="gpt-5", spec=find_by_name("openai"),
+ )
+ result = await provider.chat(
+ messages=[{"role": "user", "content": "Search issues"}],
+ tools=[{"type": "function", "function": {
+ "name": "list_issues", "parameters": parameters,
+ }}],
+ )
+
+ assert result.content == "ok"
+ mock_responses.assert_awaited_once()
+ tool = mock_responses.call_args.kwargs["tools"][0]
+ assert tool["strict"] is False
+ assert tool["parameters"] == parameters
diff --git a/tests/providers/test_openai_codex_provider.py b/tests/providers/test_openai_codex_provider.py
index 912360918..79d248c4d 100644
--- a/tests/providers/test_openai_codex_provider.py
+++ b/tests/providers/test_openai_codex_provider.py
@@ -998,3 +998,29 @@ async def test_codex_stream_surfaces_reasoning_summary(monkeypatch) -> None:
async def _append(target: list[str], value: str) -> None:
target.append(value)
+
+
+async def test_codex_request_preserves_optional_tool_fields(monkeypatch):
+ _mock_codex_token(monkeypatch)
+ captured = {}
+
+ async def fake_request(_url, _headers, body, **_kwargs):
+ captured.update(body)
+ return provider_base.LLMResponse(content="ok")
+
+ monkeypatch.setattr("nanobot.providers.openai_codex_provider._request_codex", fake_request)
+ parameters = {
+ "type": "object",
+ "properties": {"query": {"type": "string"}},
+ "required": [],
+ }
+ response = await OpenAICodexProvider().chat(
+ [{"role": "user", "content": "Search issues"}],
+ tools=[{"type": "function", "function": {
+ "name": "list_issues", "parameters": parameters,
+ }}],
+ )
+
+ assert response.content == "ok"
+ assert captured["tools"][0]["strict"] is False
+ assert captured["tools"][0]["parameters"] == parameters
diff --git a/tests/providers/test_openai_responses.py b/tests/providers/test_openai_responses.py
index 004c3e104..8ceadbff5 100644
--- a/tests/providers/test_openai_responses.py
+++ b/tests/providers/test_openai_responses.py
@@ -383,6 +383,29 @@ class TestConvertTools:
assert result[0]["description"] == "Get weather"
assert "properties" in result[0]["parameters"]
+ @pytest.mark.parametrize("wrapped", [False, True])
+ @pytest.mark.parametrize("strict", [None, False, True])
+ def test_preserves_strict_and_optional_parameters(self, wrapped, strict):
+ parameters = {
+ "type": "object",
+ "properties": {
+ "query": {"type": "string"},
+ "customView": {"type": "string", "minLength": 1},
+ },
+ "required": [],
+ }
+ function = {"name": "list_issues", "parameters": parameters}
+ if strict is not None:
+ function["strict"] = strict
+ tool = {"type": "function", "function": function} if wrapped else function
+
+ result = convert_tools([tool])[0]
+
+ assert result["strict"] is (strict if strict is not None else False)
+ assert result["parameters"] == parameters
+ assert parameters["required"] == []
+ assert ("strict" in function) is (strict is not None)
+
def test_tool_without_name_skipped(self):
tools = [{"type": "function", "function": {"parameters": {}}}]
assert convert_tools(tools) == []
diff --git a/tests/providers/test_xai_grok_provider.py b/tests/providers/test_xai_grok_provider.py
index dd646304f..7abf46d12 100644
--- a/tests/providers/test_xai_grok_provider.py
+++ b/tests/providers/test_xai_grok_provider.py
@@ -164,6 +164,7 @@ async def test_provider_injects_hosted_x_search_and_required_proxy_headers(monke
"name": "read_file",
"description": "Read a file",
"parameters": {"type": "object"},
+ "strict": False,
},
{"type": "x_search"},
]
@@ -242,6 +243,7 @@ async def test_explicit_parameterized_x_search_is_preserved_without_catalog_look
"name": "read_file",
"description": "Read a file",
"parameters": {"type": "object"},
+ "strict": False,
},
hosted_tool,
{"type": "code_interpreter", "container": "auto"},
@@ -288,6 +290,7 @@ async def test_explicit_empty_tools_disables_catalog_lookup_and_hosted_tool(monk
"name": "read_file",
"description": "Read a file",
"parameters": {"type": "object"},
+ "strict": False,
}
]
assert "max_turns" not in bodies[0]
@@ -327,6 +330,7 @@ async def test_provider_keeps_local_x_search_when_model_does_not_support_hosted_
"name": "x_search",
"description": "A local search fallback",
"parameters": {"type": "object"},
+ "strict": False,
}
]
assert "max_turns" not in bodies[0]
diff --git a/tests/tools/test_mcp_tool.py b/tests/tools/test_mcp_tool.py
index 636a12132..a6d6dc516 100644
--- a/tests/tools/test_mcp_tool.py
+++ b/tests/tools/test_mcp_tool.py
@@ -2107,3 +2107,43 @@ def test_long_server_name_tools_are_matched_by_server_name() -> None:
assert removed == 1
assert wrapper.name not in registry.tool_names
assert other_wrapper.name in registry.tool_names
+
+
[email protected]("params, error", [
+ ({"team": "nanobot", "query": "bug"}, None),
+ ({"team": "nanobot", "customView": "saved-view"}, None),
+ ({"query": "bug"}, "missing required team"),
+ ({"team": "nanobot", "customView": ""}, "customView must be at least 1 chars"),
+])
+async def test_optional_mcp_filters_reach_server_unchanged(params, error):
+ async def call_tool(name, arguments):
+ assert name == "list_issues"
+ assert not ({"query", "customView"} <= arguments.keys())
+ return SimpleNamespace(content=[_FakeTextContent("ok")])
+
+ session = SimpleNamespace(call_tool=AsyncMock(side_effect=call_tool))
+ wrapper = MCPToolWrapper(session, "linear", SimpleNamespace(
+ name="list_issues",
+ description="Search issues or use a saved view",
+ inputSchema={
+ "type": "object",
+ "properties": {
+ "team": {"type": "string"},
+ "query": {"type": "string"},
+ "customView": {"type": "string", "minLength": 1},
+ },
+ "required": ["team"],
+ },
+ ))
+ registry = ToolRegistry()
+ registry.register(wrapper)
+
+ result = await registry.execute(wrapper.name, params)
+
+ if error:
+ assert is_tool_error_result(result)
+ assert error in result
+ session.call_tool.assert_not_awaited()
+ else:
+ assert result == "ok"
+ session.call_tool.assert_awaited_once_with("list_issues", arguments=params)