fix(ollama): keep replayed tool arguments out of a rejected replay's error chain

This commit is contained in:
mateo-berri 2026-11-06 19:12:13 -08:00
parent e6044318cc
commit 0f7003a2ae
2 changed files with 10 additions and 6 deletions

View file

@ -55,18 +55,18 @@ def _to_ollama_tool_call_function(model: str, tool_call: ChatCompletionAssistant
name: Final = function.get("name") or ""
raw_arguments: Final[object] = function.get("arguments")
try:
parsed: Final = (
parsed: Final[object] = (
parse_tool_call_arguments(raw_arguments, tool_name=name, context="Ollama chat request")
if isinstance(raw_arguments, str) or raw_arguments is None
else raw_arguments
)
arguments: Final = _TOOL_CALL_ARGUMENTS.validate_python({} if parsed is None else parsed)
except ValueError as e:
except ValueError:
raise litellm.BadRequestError(
message=f"OllamaException - arguments of assistant tool call {tool_call.get('id')!r} are not a JSON object",
model=model,
llm_provider="ollama_chat",
) from e
) from None
return OllamaToolCallFunction(name=name, arguments=arguments)

View file

@ -4,6 +4,7 @@ import itertools
import logging
import os
import sys
import traceback
from typing import Final, cast
import pytest
@ -1099,10 +1100,12 @@ class TestOllamaToolCallTransformation:
@pytest.mark.parametrize(
"arguments",
['{"path": "a.rs"}{"path": "b.rs"}', '["a.rs", "b.rs"]', '[["path", "a.rs"]]'],
ids=["glued", "array", "pairs"],
['{"path": "a.rs"}{"path": "b.rs"}', '["a.rs", "b.rs"]', '[["path", "a.rs"]]', "[]"],
ids=["glued", "array", "pairs", "empty_array"],
)
def test_replayed_tool_call_arguments_that_are_not_an_object_answer_400(self, arguments: str):
def test_replayed_tool_call_arguments_that_are_not_an_object_answer_400_without_echoing_them(
self, arguments: str
):
with pytest.raises(litellm.BadRequestError) as exc_info:
litellm.completion(
model="ollama_chat/qwen3:8b",
@ -1112,6 +1115,7 @@ class TestOllamaToolCallTransformation:
assert exc_info.value.status_code == 400
assert "call_abc123" in str(exc_info.value)
assert "a.rs" not in "".join(traceback.format_exception(exc_info.value))
class TestOllamaStreamingParallelToolCallIndex: