(Ollama) Fix String Value parsing in _parse_arguments_from_tool_call (#30154)

- **Description:** Fix String Value parsing in
_parse_arguments_from_tool_call
- **Issue:** #30145

---------

Co-authored-by: Chester Curme <chester.curme@gmail.com>
This commit is contained in:
Mohammad Mohtashim 2025-03-20 06:47:18 +05:00 committed by GitHub
parent c0ffc9aa29
commit 1103bdfaf1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 16 additions and 4 deletions

View File

@ -125,13 +125,17 @@ def _parse_arguments_from_tool_call(
if "function" not in raw_tool_call:
return None
arguments = raw_tool_call["function"]["arguments"]
parsed_arguments = {}
parsed_arguments: dict = {}
if isinstance(arguments, dict):
for key, value in arguments.items():
if isinstance(value, str):
parsed_arguments[key] = _parse_json_string(
parsed_value = _parse_json_string(
value, skip=True, raw_tool_call=raw_tool_call
)
if isinstance(parsed_value, (dict, list)):
parsed_arguments[key] = parsed_value
else:
parsed_arguments[key] = value
else:
parsed_arguments[key] = value
else:

View File

@ -1,10 +1,10 @@
"""Test chat model integration."""
import json
from typing import Dict, Type
from langchain_tests.unit_tests import ChatModelUnitTests
from langchain_ollama.chat_models import ChatOllama
from langchain_ollama.chat_models import ChatOllama, _parse_arguments_from_tool_call
class TestChatOllama(ChatModelUnitTests):
@ -15,3 +15,11 @@ class TestChatOllama(ChatModelUnitTests):
@property
def chat_model_params(self) -> Dict:
return {"model": "llama3-groq-tool-use"}
def test__parse_arguments_from_tool_call() -> None:
raw_response = '{"model":"sample-model","message":{"role":"assistant","content":"","tool_calls":[{"function":{"name":"get_profile_details","arguments":{"arg_1":"12345678901234567890123456"}}}]},"done":false}' # noqa: E501
raw_tool_calls = json.loads(raw_response)["message"]["tool_calls"]
response = _parse_arguments_from_tool_call(raw_tool_calls[0])
assert response is not None
assert isinstance(response["arg_1"], str)