The agent engineering platform.
Python agentsaiai-agentsanthropicchatgptdeepagentsenterpriseframeworkgeminigenerative-ailangchainlanggraphllmmultiagentopen-sourceopenaipydanticpythonragtypescript
fix(ollama): robustly parse single-quoted JSON in tool calls (#32109)
diego-coder8e4396bb32c0
diff --git a/libs/langchain/pyproject.toml b/libs/langchain/pyproject.tomlindex a373c842cd..1b6c4f055b 100644--- a/libs/langchain/pyproject.toml+++ b/libs/langchain/pyproject.toml@@ -5,7 +5,7 @@ build-backend = "pdm.backend"[project]authors = []license = { text = "MIT" }-requires-python = ">=3.9"+requires-python = ">=3.9, <4.0"dependencies = ["langchain-core<1.0.0,>=0.3.66","langchain-text-splitters<1.0.0,>=0.3.8",diff --git a/libs/partners/ollama/langchain_ollama/chat_models.py b/libs/partners/ollama/langchain_ollama/chat_models.pyindex b987e8492d..4fbe9bce06 100644--- a/libs/partners/ollama/langchain_ollama/chat_models.py+++ b/libs/partners/ollama/langchain_ollama/chat_models.py@@ -2,6 +2,7 @@from __future__ import annotations+import astimport jsonfrom collections.abc import AsyncIterator, Iterator, Mapping, Sequencefrom operator import itemgetter@@ -77,33 +78,45 @@ def _get_usage_metadata_from_generation_info(def _parse_json_string(json_string: str,+ *,raw_tool_call: dict[str, Any],- skip: bool, # noqa: FBT001+ skip: bool,) -> Any:"""Attempt to parse a JSON string for tool calling.+ It first tries to use the standard json.loads. If that fails, it falls+ back to ast.literal_eval to safely parse Python literals, which is more+ robust against models using single quotes or containing apostrophes.+Args:json_string: JSON string to parse.- skip: Whether to ignore parsing errors and return the value anyways.raw_tool_call: Raw tool call to include in error message.+ skip: Whether to ignore parsing errors and return the value anyways.Returns:- The parsed JSON string.+ The parsed JSON string or Python literal.Raises:- OutputParserException: If the JSON string wrong invalid and skip=False.+ OutputParserException: If the string is invalid and skip=False."""try:return json.loads(json_string)- except json.JSONDecodeError as e:- if skip:- return json_string- msg = (- f"Function {raw_tool_call['function']['name']} arguments:\n\n"- f"{raw_tool_call['function']['arguments']}\n\nare not valid JSON. "- f"Received JSONDecodeError {e}"- )- raise OutputParserException(msg) from e+ except json.JSONDecodeError:+ try:+ # Use ast.literal_eval to safely parse Python-style dicts+ # (e.g. with single quotes)+ return ast.literal_eval(json_string)+ except (SyntaxError, ValueError) as e:+ # If both fail, and we're not skipping, raise an informative error.+ if skip:+ return json_string+ msg = (+ f"Function {raw_tool_call['function']['name']} arguments:\n\n"+ f"{raw_tool_call['function']['arguments']}"+ "\n\nare not valid JSON or a Python literal. "+ f"Received error: {e}"+ )+ raise OutputParserException(msg) from eexcept TypeError as e:if skip:return json_stringdiff --git a/libs/partners/ollama/tests/unit_tests/test_chat_models.py b/libs/partners/ollama/tests/unit_tests/test_chat_models.pyindex e7ac5a2995..3a856a4883 100644--- a/libs/partners/ollama/tests/unit_tests/test_chat_models.py+++ b/libs/partners/ollama/tests/unit_tests/test_chat_models.py@@ -8,10 +8,15 @@ from unittest.mock import patchimport pytestfrom httpx import Client, Request, Response+from langchain_core.exceptions import OutputParserExceptionfrom langchain_core.messages import ChatMessagefrom langchain_tests.unit_tests import ChatModelUnitTests-from langchain_ollama.chat_models import ChatOllama, _parse_arguments_from_tool_call+from langchain_ollama.chat_models import (+ ChatOllama,+ _parse_arguments_from_tool_call,+ _parse_json_string,+)MODEL_NAME = "llama3.1"@@ -49,13 +54,11 @@ def test_arbitrary_roles_accepted_in_chatmessages(monkeypatch: pytest.MonkeyPatch,) -> None:monkeypatch.setattr(Client, "stream", _mock_httpx_client_stream)-llm = ChatOllama(model=MODEL_NAME,verbose=True,format=None,)-messages = [ChatMessage(role="somerandomrole",@@ -64,7 +67,6 @@ def test_arbitrary_roles_accepted_in_chatmessages(ChatMessage(role="control", content="thinking"),ChatMessage(role="user", content="What is the meaning of life?"),]-llm.invoke(messages)@@ -83,3 +85,58 @@ def test_validate_model_on_init(mock_validate_model: Any) -> None:# Test that validate_model is NOT called by defaultChatOllama(model=MODEL_NAME)mock_validate_model.assert_not_called()+++# Define a dummy raw_tool_call for the function signature+dummy_raw_tool_call = {+ "function": {"name": "test_func", "arguments": ""},+}+++# --- Regression tests for tool-call argument parsing (see #30910) ---+++ "input_string, expected_output",+ [+ # Case 1: Standard double-quoted JSON+ ('{"key": "value", "number": 123}', {"key": "value", "number": 123}),+ # Case 2: Single-quoted string (the original bug)+ ("{'key': 'value', 'number': 123}", {"key": "value", "number": 123}),+ # Case 3: String with an internal apostrophe+ ('{"text": "It\'s a great test!"}', {"text": "It's a great test!"}),+ # Case 4: Mixed quotes that ast can handle+ ("{'text': \"It's a great test!\"}", {"text": "It's a great test!"}),+ ],+)+def test_parse_json_string_success_cases(+ input_string: str, expected_output: Any+) -> None:+ """Tests that _parse_json_string correctly parses valid and fixable strings."""+ raw_tool_call = {"function": {"name": "test_func", "arguments": input_string}}+ result = _parse_json_string(input_string, raw_tool_call=raw_tool_call, skip=False)+ assert result == expected_output+++def test_parse_json_string_failure_case_raises_exception() -> None:+ """Tests that _parse_json_string raises an exception for truly malformed strings."""+ malformed_string = "{'key': 'value',,}"+ raw_tool_call = {"function": {"name": "test_func", "arguments": malformed_string}}+ with pytest.raises(OutputParserException):+ _parse_json_string(+ malformed_string,+ raw_tool_call=raw_tool_call,+ skip=False,+ )+++def test_parse_json_string_skip_returns_input_on_failure() -> None:+ """Tests that skip=True returns the original string on parse failure."""+ malformed_string = "{'not': valid,,,}"+ raw_tool_call = {"function": {"name": "test_func", "arguments": malformed_string}}+ result = _parse_json_string(+ malformed_string,+ raw_tool_call=raw_tool_call,+ skip=True,+ )+ assert result == malformed_string