Skip to content

Commit 1e2d589

Browse files
diego-coderRN
authored andcommitted
feat: improve JSON arg parsing in ChatMLX
1 parent aa98e47 commit 1e2d589

File tree

1 file changed

+23
-0
lines changed
  • libs/community/langchain_community/chat_models

1 file changed

+23
-0
lines changed

libs/community/langchain_community/chat_models/mlx.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
"""MLX Chat Wrapper."""
22

3+
import json
4+
import logging
35
from typing import (
46
Any,
57
Callable,
@@ -38,6 +40,8 @@
3840

3941
from langchain_community.llms.mlx_pipeline import MLXPipeline
4042

43+
logger = logging.getLogger(__name__)
44+
4145
DEFAULT_SYSTEM_PROMPT = """You are a helpful, respectful, and honest assistant."""
4246

4347

@@ -69,6 +73,25 @@ def __init__(self, **kwargs: Any):
6973
super().__init__(**kwargs)
7074
self.tokenizer = self.llm.tokenizer
7175

76+
def _parse_tool_args(self, arg_text: str) -> Dict[str, Any]:
77+
"""Parse the arguments for a tool call.
78+
79+
Args:
80+
arg_text: JSON string representation of the tool arguments.
81+
82+
Returns:
83+
Parsed arguments dictionary. If parsing fails, returns a dict with
84+
the original text under the ``input`` key.
85+
"""
86+
try:
87+
args = json.loads(arg_text)
88+
except json.JSONDecodeError:
89+
args = {"input": arg_text}
90+
except Exception as e: # pragma: no cover - defensive
91+
logger.warning("Unexpected error during tool argument parsing: %s", e)
92+
args = {"input": arg_text}
93+
return args
94+
7295
def _generate(
7396
self,
7497
messages: List[BaseMessage],

0 commit comments

Comments
 (0)