_parse_json_string() — langchain Function Reference
Architecture documentation for the _parse_json_string() function in chat_models.py from the langchain codebase.
Entity Profile
Dependency Diagram
graph TD e6c4a9df_6037_7280_75cd_a57b75d96dfe["_parse_json_string()"] 5b41b5a0_b415_2ecd_9522_191ca69202ca["chat_models.py"] e6c4a9df_6037_7280_75cd_a57b75d96dfe -->|defined in| 5b41b5a0_b415_2ecd_9522_191ca69202ca ef2fbbf4_d176_ecab_7ad5_9a91394db4ee["_parse_arguments_from_tool_call()"] ef2fbbf4_d176_ecab_7ad5_9a91394db4ee -->|calls| e6c4a9df_6037_7280_75cd_a57b75d96dfe style e6c4a9df_6037_7280_75cd_a57b75d96dfe fill:#6366f1,stroke:#818cf8,color:#fff
Relationship Graph
Source Code
libs/partners/ollama/langchain_ollama/chat_models.py lines 118–167
def _parse_json_string(
json_string: str,
*,
raw_tool_call: dict[str, Any],
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.
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 or Python literal.
Raises:
OutputParserException: If the string is invalid and `skip=False`.
"""
try:
return json.loads(json_string)
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 e
except TypeError 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 a string or a "
f"dictionary. Received TypeError {e}"
)
raise OutputParserException(msg) from e
Domain
Subdomains
Called By
Source
Frequently Asked Questions
What does _parse_json_string() do?
_parse_json_string() is a function in the langchain codebase, defined in libs/partners/ollama/langchain_ollama/chat_models.py.
Where is _parse_json_string() defined?
_parse_json_string() is defined in libs/partners/ollama/langchain_ollama/chat_models.py at line 118.
What calls _parse_json_string()?
_parse_json_string() is called by 1 function(s): _parse_arguments_from_tool_call.
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free