mirror of
https://github.com/langchain-ai/langchain.git
synced 2026-10-05 09:25:14 +03:00
fix(openai): filter invalid tool calls from content (#39366)
This commit is contained in:
1 parent
9f738f5297
commit
9b4ef2ab34
2 files changed
+27
-1
No files matched your search
@@ -165,7 +165,7 @@ def _convert_from_v1_to_chat_completions(message: AIMessage) -> AIMessage:
|
||||
if block_type == "text":
|
||||
# Strip annotations
|
||||
new_content.append({"type": "text", "text": block["text"]})
|
||||
elif block_type in ("reasoning", "tool_call"):
|
||||
elif block_type in ("reasoning", "tool_call", "invalid_tool_call"):
|
||||
pass
|
||||
else:
|
||||
new_content.append(block)
|
||||
|
||||
@@ -3643,6 +3643,13 @@ def test_compat_responses_v03_apply_patch_tool_outputs() -> None:
|
||||
"name": "get_weather",
|
||||
"args": {"location": "San Francisco"},
|
||||
},
|
||||
{
|
||||
"type": "invalid_tool_call",
|
||||
"id": "call_234",
|
||||
"name": "get_weather",
|
||||
"args": '{"location":',
|
||||
"error": "Failed to parse tool call arguments as JSON",
|
||||
},
|
||||
{
|
||||
"type": "text",
|
||||
"text": "Hello, world!",
|
||||
@@ -3653,11 +3660,29 @@ def test_compat_responses_v03_apply_patch_tool_outputs() -> None:
|
||||
],
|
||||
id="chatcmpl-123",
|
||||
response_metadata={"model_provider": "openai", "model_name": "gpt-4.1"},
|
||||
invalid_tool_calls=[
|
||||
InvalidToolCall(
|
||||
id="call_234",
|
||||
name="get_weather",
|
||||
args='{"location":',
|
||||
error="Failed to parse tool call arguments as JSON",
|
||||
type="invalid_tool_call",
|
||||
)
|
||||
],
|
||||
),
|
||||
AIMessage(
|
||||
[{"type": "text", "text": "Hello, world!"}],
|
||||
id="chatcmpl-123",
|
||||
response_metadata={"model_provider": "openai", "model_name": "gpt-4.1"},
|
||||
invalid_tool_calls=[
|
||||
InvalidToolCall(
|
||||
id="call_234",
|
||||
name="get_weather",
|
||||
args='{"location":',
|
||||
error="Failed to parse tool call arguments as JSON",
|
||||
type="invalid_tool_call",
|
||||
)
|
||||
],
|
||||
),
|
||||
)
|
||||
],
|
||||
@@ -3668,6 +3693,7 @@ def test_convert_from_v1_to_chat_completions(
|
||||
result = _convert_from_v1_to_chat_completions(message_v1)
|
||||
assert result == expected
|
||||
assert result.tool_calls == message_v1.tool_calls # tool calls remain cached
|
||||
assert result.invalid_tool_calls == message_v1.invalid_tool_calls
|
||||
|
||||
# Check no mutation
|
||||
assert message_v1 != result
|
||||
|
||||
Reference in new issue
Block a user