fix(llm): reject invalid tool arguments

This commit is contained in:
Iams4kura
2026-09-04 11:29:14 +08:00
parent 4a9bdff518
commit 8eedb3d3de
2 changed files with 32 additions and 3 deletions
+6 -1
View File
@@ -22,12 +22,17 @@ def _to_openai_tool_call_param(
tool_call: ToolCall,
) -> ChatCompletionMessageToolCallParam:
"""Convert a domain tool call to the OpenAI request schema."""
try:
arguments = json.dumps(tool_call.arguments, allow_nan=False)
except (TypeError, ValueError) as exc:
raise ValueError("OpenAI tool call arguments must be valid JSON") from exc
return {
"id": tool_call.id,
"type": "function",
"function": {
"name": tool_call.name,
"arguments": json.dumps(tool_call.arguments),
"arguments": arguments,
},
}
+26 -2
View File
@@ -145,12 +145,36 @@ def test_openai_compatible_provider_serializes_assistant_tool_calls(
{"role": "tool", "content": "result", "tool_call_id": "call_1"},
]
client.completions.params = None
submitted_params = client.completions.params
with pytest.raises(ValueError, match="tool messages require a tool_call_id"):
provider.generate(
LLMInput(messages=[Message(role=Role.TOOL, content="unlinked result")])
)
assert client.completions.params is None
assert client.completions.params is submitted_params
for invalid_arguments in ({"score": float("nan")}, {"value": object()}):
with pytest.raises(
ValueError,
match="tool call arguments must be valid JSON",
):
provider.generate(
LLMInput(
messages=[
Message(
role=Role.ASSISTANT,
content="",
tool_calls=[
ToolCall(
id="call_invalid",
name="score",
arguments=invalid_arguments,
)
],
)
]
)
)
assert client.completions.params is submitted_params
def test_openai_provider_can_be_constructed_without_credentials(monkeypatch):