mirror of
https://github.com/affaan-m/ECC.git
synced 2026-10-18 14:07:33 +02:00
fix(llm): reject invalid tool arguments
This commit is contained in:
@@ -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,
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
@@ -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):
|
||||
|
||||
Reference in New Issue
Block a user