Skip to content

Commit 108e148

Browse files
authored
fix #2181 FunctionTool.strict_json_schema is missing when using Chat Completions API (#2182)
1 parent 6f2bcb9 commit 108e148

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

src/agents/models/chatcmpl_converter.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -598,6 +598,7 @@ def tool_to_openai(cls, tool: Tool) -> ChatCompletionToolParam:
598598
"name": tool.name,
599599
"description": tool.description or "",
600600
"parameters": tool.params_json_schema,
601+
"strict": tool.strict_json_schema,
601602
},
602603
}
603604

@@ -614,5 +615,6 @@ def convert_handoff_tool(cls, handoff: Handoff[Any, Any]) -> ChatCompletionToolP
614615
"name": handoff.tool_name,
615616
"description": handoff.tool_description,
616617
"parameters": handoff.input_json_schema,
618+
"strict": handoff.strict_json_schema,
617619
},
618620
}

tests/test_tool_converter.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,23 @@ def test_to_openai_with_function_tool():
1818
result = Converter.tool_to_openai(tool)
1919

2020
assert result["type"] == "function"
21-
assert result["function"]["name"] == "some_function"
22-
params = result.get("function", {}).get("parameters")
21+
function_def = result["function"]
22+
assert function_def["name"] == "some_function"
23+
assert function_def["strict"] is True
24+
params = function_def.get("parameters")
2325
assert params is not None
2426
properties = params.get("properties", {})
2527
assert isinstance(properties, dict)
2628
assert properties.keys() == {"a", "b"}
2729

2830

31+
def test_to_openai_respects_non_strict_function_tool():
32+
tool = function_tool(some_function, strict_mode=False)
33+
result = Converter.tool_to_openai(tool)
34+
35+
assert result["function"]["strict"] is False
36+
37+
2938
class Foo(BaseModel):
3039
a: str
3140
b: list[int]
@@ -39,6 +48,7 @@ def test_convert_handoff_tool():
3948
assert result["type"] == "function"
4049
assert result["function"]["name"] == Handoff.default_tool_name(agent)
4150
assert result["function"].get("description") == Handoff.default_tool_description(agent)
51+
assert result["function"].get("strict") is True
4252
params = result.get("function", {}).get("parameters")
4353
assert params is not None
4454

0 commit comments

Comments
 (0)