Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ollama tools argument type modification #30145

Open
5 tasks done
pawelka opened this issue Mar 7, 2025 · 0 comments
Open
5 tasks done

Ollama tools argument type modification #30145

pawelka opened this issue Mar 7, 2025 · 0 comments

Comments

@pawelka
Copy link

pawelka commented Mar 7, 2025

Checked other resources

  • I added a very descriptive title to this issue.
  • I searched the LangChain documentation with the integrated search.
  • I used the GitHub search to find a similar question and didn't find it.
  • I am sure that this is a bug in LangChain rather than my code.
  • The bug is not resolved by updating to the latest stable version of LangChain (or the specific integration package).

Example Code

import json
from langchain_ollama.chat_models import _parse_arguments_from_tool_call

raw_response = """{"model":"sample-model","message":{"role":"assistant","content":"","tool_calls":[{"function":{"name":"get_profile_details","arguments":{"xxxyyy":"12345678901234567890123456"}}}]},"done":false}"""
raw_tool_calls = json.loads(raw_response)['message']['tool_calls']
_parse_arguments_from_tool_call(raw_tool_calls[0])

# result: {'xxxyyy': 12345678901234567890123456}

#expected {'xxxyyy': '12345678901234567890123456'}

Error Message and Stack Trace (if applicable)

Error is cause by pydantic that tool expect string but received int.

Description

Change in ticket #28225 cause that simple argument like string with only digits only are convert to int and because it's long string with digits part of value is lost.

After commenting changes from related ticket:

def _parse_arguments_from_tool_call(
    raw_tool_call: dict[str, Any],
) -> Optional[dict[str, Any]]:
    """Parse arguments by trying to parse any shallowly nested string-encoded JSON.

    Band-aid fix for issue in Ollama with inconsistent tool call argument structure.
    Should be removed/changed if fixed upstream.
    See https://github.com/ollama/ollama/issues/6155
    """
    if "function" not in raw_tool_call:
        return None
    arguments = raw_tool_call["function"]["arguments"]
    parsed_arguments = {}
    if isinstance(arguments, dict):
        for key, value in arguments.items():
            # if isinstance(value, str):
            #     parsed_arguments[key] = _parse_json_string(
            #         value, skip=True, raw_tool_call=raw_tool_call
            #     )
            # else:
            parsed_arguments[key] = value
    else:
        parsed_arguments = _parse_json_string(
            arguments, skip=False, raw_tool_call=raw_tool_call
        )
    return parsed_arguments


problem doesn't exists. When debugging noticed that llm result (tools argument) are converted to dict and in dict I have 'value': '12345678901234567890' but after parsing  '12345678901234567890' to json (method) _parse_json_string it's converted to int even if tool expected argument is string. 

Two potential solutions:
1. check expected type by tool and if it's match doesn't parse as json
2. Parse only as JSON if at least basic json pattern is matched and it's no value only

### System Info


System Information
------------------
> OS:  Darwin
> OS Version:  Darwin Kernel Version 24.3.0: Thu Jan  2 20:24:24 PST 2025; root:xnu-11215.81.4~3/RELEASE_ARM64_T6030
> Python Version:  3.13.2 (main, Feb  4 2025, 14:51:09) [Clang 16.0.0 (clang-1600.0.26.6)]

Package Information
-------------------
> langchain_core: 0.3.34
> langchain: 0.3.17
> langchain_community: 0.3.16
> langsmith: 0.3.6
> langchain_azure_ai: 0.1.0
> langchain_experimental: 0.3.4
> langchain_ollama: 0.2.3
> langchain_openai: 0.3.4
> langchain_text_splitters: 0.3.6
> langgraph_sdk: 0.1.51

Optional packages not installed
-------------------------------
> langserve

Other Dependencies
------------------
> aiohttp: 3.11.12
> async-timeout: Installed. No version info available.
> azure-ai-inference[opentelemetry]: Installed. No version info available.
> azure-core: 1.32.0
> azure-identity: 1.19.0
> azure-monitor-opentelemetry: Installed. No version info available.
> dataclasses-json: 0.6.7
> httpx: 0.28.1
> httpx-sse: 0.4.0
> jsonpatch<2.0,>=1.33: Installed. No version info available.
> langchain-core<1.0.0,>=0.3.34: Installed. No version info available.
> langsmith-pyo3: Installed. No version info available.
> langsmith<0.4,>=0.1.125: Installed. No version info available.
> numpy: 2.2.2
> ollama: 0.4.7
> openai<2.0.0,>=1.58.1: Installed. No version info available.
> opentelemetry-instrumentation-threading: Installed. No version info available.
> opentelemetry-semantic-conventions-ai: Installed. No version info available.
> orjson: 3.10.15
> packaging<25,>=23.2: Installed. No version info available.
> pydantic: 2.10.6
> pydantic-settings: 2.7.1
> pydantic<3.0.0,>=2.5.2;: Installed. No version info available.
> pydantic<3.0.0,>=2.7.4;: Installed. No version info available.
> pytest: Installed. No version info available.
> PyYAML: 6.0.2
> PyYAML>=5.3: Installed. No version info available.
> requests: 2.32.3
> requests-toolbelt: 1.0.0
> rich: 13.9.4
> SQLAlchemy: 2.0.38
> tenacity: 9.0.0
> tenacity!=8.4.0,<10.0.0,>=8.1.0: Installed. No version info available.
> tiktoken<1,>=0.7: Installed. No version info available.
> typing-extensions>=4.7: Installed. No version info available.
> zstandard: 0.23.0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant