Skip to content

The agent engineering platform.

fix(core): handle None arguments in parse_tool_call (#34242)

rari40415cc090e5228
diff --git a/libs/core/langchain_core/output_parsers/openai_tools.py b/libs/core/langchain_core/output_parsers/openai_tools.py
index 6d2d325d90..037afd4312 100644
--- a/libs/core/langchain_core/output_parsers/openai_tools.py
+++ b/libs/core/langchain_core/output_parsers/openai_tools.py
@@ -47,22 +47,24 @@ def parse_tool_call(
"""
if "function" not in raw_tool_call:
return None
+
+ arguments = raw_tool_call["function"]["arguments"]
+
if partial:
try:
- function_args = parse_partial_json(
- raw_tool_call["function"]["arguments"], strict=strict
- )
+ function_args = parse_partial_json(arguments, strict=strict)
except (JSONDecodeError, TypeError): # None args raise TypeError
return None
+ # Handle None or empty string arguments for parameter-less tools
+ elif not arguments:
+ function_args = {}
else:
try:
- function_args = json.loads(
- raw_tool_call["function"]["arguments"], strict=strict
- )
+ function_args = json.loads(arguments, strict=strict)
except JSONDecodeError as e:
msg = (
f"Function {raw_tool_call['function']['name']} arguments:\n\n"
- f"{raw_tool_call['function']['arguments']}\n\nare not valid JSON. "
+ f"{arguments}\n\nare not valid JSON. "
f"Received JSONDecodeError {e}"
)
raise OutputParserException(msg) from e
diff --git a/libs/core/tests/unit_tests/output_parsers/test_openai_tools.py b/libs/core/tests/unit_tests/output_parsers/test_openai_tools.py
index 1ae19b8b55..57b533f3a6 100644
--- a/libs/core/tests/unit_tests/output_parsers/test_openai_tools.py
+++ b/libs/core/tests/unit_tests/output_parsers/test_openai_tools.py
@@ -16,6 +16,7 @@ from langchain_core.output_parsers.openai_tools import (
JsonOutputKeyToolsParser,
JsonOutputToolsParser,
PydanticToolsParser,
+ parse_tool_call,
)
from langchain_core.outputs import ChatGeneration
@@ -1345,3 +1346,76 @@ def test_pydantic_tools_parser_with_optional_fields() -> None:
assert result_mixed[1].username == "alice"
assert result_mixed[1].bio is None
assert result_mixed[1].age == 35
+
+
+def test_parse_tool_call_with_none_arguments() -> None:
+ """Test parse_tool_call handles None arguments for parameter-less tools.
+
+ When an LLM calls a tool that has no parameters, some providers return
+ None for the arguments field instead of an empty string or "{}".
+ This should not raise an error.
+
+ See: https://github.com/langchain-ai/langchain/issues/34123
+ """
+ # Test case from issue #34123: arguments is None
+ raw_tool_call = {
+ "function": {"arguments": None, "name": "orderStatus"},
+ "id": "chatcmpl-tool-8b1f759d874b412e931e64cf6f57bdcc",
+ "type": "function",
+ }
+
+ # This should not raise an error - should return parsed tool call with empty args
+ result = parse_tool_call(raw_tool_call, return_id=True)
+
+ assert result is not None
+ assert result["name"] == "orderStatus"
+ assert result["args"] == {}
+ assert result["id"] == "chatcmpl-tool-8b1f759d874b412e931e64cf6f57bdcc"
+
+
+def test_parse_tool_call_with_empty_string_arguments() -> None:
+ """Test parse_tool_call handles empty string arguments."""
+ raw_tool_call = {
+ "function": {"arguments": "", "name": "getStatus"},
+ "id": "call_123",
+ "type": "function",
+ }
+
+ # Empty string should be treated as empty args
+ result = parse_tool_call(raw_tool_call, return_id=True)
+
+ assert result is not None
+ assert result["name"] == "getStatus"
+ assert result["args"] == {}
+ assert result["id"] == "call_123"
+
+
+def test_parse_tool_call_with_valid_arguments() -> None:
+ """Test parse_tool_call works normally with valid JSON arguments."""
+ raw_tool_call = {
+ "function": {"arguments": '{"param": "value"}', "name": "myTool"},
+ "id": "call_456",
+ "type": "function",
+ }
+
+ result = parse_tool_call(raw_tool_call, return_id=True)
+
+ assert result is not None
+ assert result["name"] == "myTool"
+ assert result["args"] == {"param": "value"}
+ assert result["id"] == "call_456"
+
+
+def test_parse_tool_call_partial_mode_with_none_arguments() -> None:
+ """Test parse_tool_call in partial mode handles None arguments."""
+ raw_tool_call = {
+ "function": {"arguments": None, "name": "streamingTool"},
+ "id": "call_789",
+ "type": "function",
+ }
+
+ # Partial mode should return None for None arguments (existing behavior)
+ result = parse_tool_call(raw_tool_call, partial=True, return_id=True)
+
+ # In partial mode, None arguments returns None (incomplete tool call)
+ assert result is None