fix(providers): preserve tool optionality in Responses requests
This commit is contained in:
1 parent
f62e0da9f6
commit
932b06f0de
8 files changed
+127
-1
No files matched your search
@@ -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
|
||||
|
||||
|
||||
@@ -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"
|
||||
|
||||
|
||||
|
||||
@@ -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:
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
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
|
||||
@@ -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
|
||||
@@ -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) == []
|
||||
|
||||
@@ -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]
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
@pytest.mark.parametrize("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)
|
||||
Reference in new issue
Block a user