From 9142165059d84f206723a306f5ed2757cd980492 Mon Sep 17 00:00:00 2001 From: Padma Date: Fri, 9 Oct 2026 11:16:00 +0000 Subject: [PATCH 1/3] fix(ollama_chat): surface structured Ollama errors instead of KeyError Ollama returns {"error": ...} for things like malformed tool call JSON. The streaming and non-streaming handlers assumed every payload had a message, so the error turned into a KeyError and reached callers as APIConnectionError. Both paths now raise OllamaError with Ollama's own message. The eval_count fallback in transform_response is also lazy now, so it no longer reads message.content when eval_count is present. --- litellm/llms/ollama/chat/transformation.py | 21 ++++- .../ollama/test_ollama_chat_transformation.py | 83 +++++++++++++++++++ 2 files changed, 101 insertions(+), 3 deletions(-) diff --git a/litellm/llms/ollama/chat/transformation.py b/litellm/llms/ollama/chat/transformation.py index 132d3a38536..524e69bfdf8 100644 --- a/litellm/llms/ollama/chat/transformation.py +++ b/litellm/llms/ollama/chat/transformation.py @@ -336,6 +336,7 @@ class OllamaChatConfig(BaseConfig): ) response_json: Final = raw_response.json() + _raise_on_error_payload(response_json) ## RESPONSE OBJECT _done_reason: Final = map_finish_reason(response_json.get("done_reason") or "stop") @@ -388,9 +389,10 @@ class OllamaChatConfig(BaseConfig): model_response.created = int(time.time()) model_response.model = "ollama_chat/" + model prompt_tokens = response_json.get("prompt_eval_count", litellm.token_counter(messages=messages)) - completion_tokens: Final = response_json.get( - "eval_count", - litellm.token_counter(text=response_json["message"]["content"]), + completion_tokens: Final = ( + response_json["eval_count"] + if "eval_count" in response_json + else litellm.token_counter(text=response_json["message"]["content"]) ) setattr( model_response, @@ -419,6 +421,17 @@ class OllamaChatConfig(BaseConfig): ) +def _raise_on_error_payload(payload: Mapping[str, object]) -> None: + error: Final = payload.get("error") + if error is None: + return + raise OllamaError( + message=str(error), + status_code=400, + headers={"Content-Type": "application/json"}, + ) + + def _done_chunk_usage(chunk: Mapping[str, object]) -> ChatCompletionUsageBlock | None: prompt_eval_count: Final = chunk.get("prompt_eval_count") eval_count: Final = chunk.get("eval_count") @@ -477,6 +490,8 @@ class OllamaChatCompletionResponseIterator(BaseModelResponseIterator): """ from litellm.types.utils import Delta, StreamingChoices + _raise_on_error_payload(chunk) + # process tool calls - if complete function arg - add id to tool call tool_calls: Final = chunk["message"].get("tool_calls") if tool_calls is not None: diff --git a/tests/unit/llms/ollama/test_ollama_chat_transformation.py b/tests/unit/llms/ollama/test_ollama_chat_transformation.py index 7d2b56e5556..b300008a142 100644 --- a/tests/unit/llms/ollama/test_ollama_chat_transformation.py +++ b/tests/unit/llms/ollama/test_ollama_chat_transformation.py @@ -16,6 +16,7 @@ from litellm.llms.ollama.chat.transformation import ( OllamaChatCompletionResponseIterator, ) +from litellm.llms.ollama.common_utils import OllamaError from litellm.types.llms.openai import AllMessageValues from litellm.utils import get_llm_provider, get_optional_params @@ -658,6 +659,88 @@ class TestOllamaFinishReasonLength: ), f"Expected 'tool_calls' when tool_calls were streamed earlier, got '{done_result.choices[0].finish_reason}'" +OLLAMA_TOOL_CALL_PARSE_ERROR = ( + "error parsing tool call: raw='{\"command\":\"grep -n flag\"]}', err=invalid character ']' after object key:value pair" +) + + +class TestOllamaStructuredErrorPayload: + def _transform(self, ollama_response: dict) -> ModelResponse: + mock_response = MagicMock() + mock_response.json.return_value = ollama_response + mock_response.text = json.dumps(ollama_response) + + model_response = ModelResponse() + model_response.choices = [Choices(message=Message(content=""), index=0)] + + return OllamaChatConfig().transform_response( + model="gpt-oss:120b", + raw_response=mock_response, + model_response=model_response, + logging_obj=MagicMock(), + request_data={}, + messages=[{"role": "user", "content": "grep for flag"}], + optional_params={}, + litellm_params={}, + encoding=None, + api_key=None, + json_mode=False, + ) + + def test_streaming_error_chunk_surfaces_ollama_message(self): + iterator = OllamaChatCompletionResponseIterator(streaming_response=iter([]), sync_stream=True) + + with pytest.raises(OllamaError) as exc_info: + iterator.chunk_parser({"error": OLLAMA_TOOL_CALL_PARSE_ERROR}) + + assert exc_info.value.message == OLLAMA_TOOL_CALL_PARSE_ERROR + assert exc_info.value.status_code == 400 + + def test_non_streaming_error_payload_surfaces_ollama_message(self): + with pytest.raises(OllamaError) as exc_info: + self._transform({"error": OLLAMA_TOOL_CALL_PARSE_ERROR}) + + assert exc_info.value.message == OLLAMA_TOOL_CALL_PARSE_ERROR + assert exc_info.value.status_code == 400 + + def test_streaming_error_chunk_does_not_mask_earlier_tool_call_state(self): + iterator = OllamaChatCompletionResponseIterator(streaming_response=iter([]), sync_stream=True) + tool_chunk = { + "model": "gpt-oss:120b", + "message": { + "role": "assistant", + "content": "", + "tool_calls": [{"function": {"name": "shell", "arguments": {"command": "ls"}}}], + }, + "done": False, + } + iterator.chunk_parser(tool_chunk) + + with pytest.raises(OllamaError) as exc_info: + iterator.chunk_parser({"error": "llama runner process has terminated: exit status 2"}) + + assert exc_info.value.message == "llama runner process has terminated: exit status 2" + + def test_usage_uses_eval_count_when_message_has_no_content(self): + result = self._transform( + { + "model": "gpt-oss:120b", + "message": { + "role": "assistant", + "tool_calls": [{"function": {"name": "shell", "arguments": {"command": "ls"}}}], + }, + "done": True, + "done_reason": "stop", + "prompt_eval_count": 11, + "eval_count": 7, + } + ) + + assert result.usage.prompt_tokens == 11 + assert result.usage.completion_tokens == 7 + assert result.usage.total_tokens == 18 + + class TestOllamaReasoningContentStreaming: """Test that reasoning_content is properly extracted from all thinking chunks.""" From fde4885a631206a897d6f0ff588578d06d43adc3 Mon Sep 17 00:00:00 2001 From: Padma Date: Fri, 9 Oct 2026 12:00:51 +0000 Subject: [PATCH 2/3] fix(ollama_chat): keep error check type-clean and annotate new tests The first version passed the untyped chunk dict into a typed helper, which added one reportUnknownArgumentType error and tripped the basedpyright gate. The check is now an in test plus a small exception builder, so no new type errors are added. The new tests also get Final annotations and a typed response parameter. --- litellm/llms/ollama/chat/transformation.py | 15 +++++++-------- .../ollama/test_ollama_chat_transformation.py | 16 ++++++++-------- 2 files changed, 15 insertions(+), 16 deletions(-) diff --git a/litellm/llms/ollama/chat/transformation.py b/litellm/llms/ollama/chat/transformation.py index 524e69bfdf8..7b255611c12 100644 --- a/litellm/llms/ollama/chat/transformation.py +++ b/litellm/llms/ollama/chat/transformation.py @@ -336,7 +336,8 @@ class OllamaChatConfig(BaseConfig): ) response_json: Final = raw_response.json() - _raise_on_error_payload(response_json) + if "error" in response_json: + raise _structured_error(f"{response_json['error']}") ## RESPONSE OBJECT _done_reason: Final = map_finish_reason(response_json.get("done_reason") or "stop") @@ -421,12 +422,9 @@ class OllamaChatConfig(BaseConfig): ) -def _raise_on_error_payload(payload: Mapping[str, object]) -> None: - error: Final = payload.get("error") - if error is None: - return - raise OllamaError( - message=str(error), +def _structured_error(message: str) -> OllamaError: + return OllamaError( + message=message, status_code=400, headers={"Content-Type": "application/json"}, ) @@ -490,7 +488,8 @@ class OllamaChatCompletionResponseIterator(BaseModelResponseIterator): """ from litellm.types.utils import Delta, StreamingChoices - _raise_on_error_payload(chunk) + if "error" in chunk: + raise _structured_error(f"{chunk['error']}") # process tool calls - if complete function arg - add id to tool call tool_calls: Final = chunk["message"].get("tool_calls") diff --git a/tests/unit/llms/ollama/test_ollama_chat_transformation.py b/tests/unit/llms/ollama/test_ollama_chat_transformation.py index b300008a142..e2fc34f82de 100644 --- a/tests/unit/llms/ollama/test_ollama_chat_transformation.py +++ b/tests/unit/llms/ollama/test_ollama_chat_transformation.py @@ -659,18 +659,18 @@ class TestOllamaFinishReasonLength: ), f"Expected 'tool_calls' when tool_calls were streamed earlier, got '{done_result.choices[0].finish_reason}'" -OLLAMA_TOOL_CALL_PARSE_ERROR = ( +OLLAMA_TOOL_CALL_PARSE_ERROR: Final = ( "error parsing tool call: raw='{\"command\":\"grep -n flag\"]}', err=invalid character ']' after object key:value pair" ) class TestOllamaStructuredErrorPayload: - def _transform(self, ollama_response: dict) -> ModelResponse: - mock_response = MagicMock() + def _transform(self, ollama_response: dict[str, object]) -> ModelResponse: + mock_response: Final = MagicMock() mock_response.json.return_value = ollama_response mock_response.text = json.dumps(ollama_response) - model_response = ModelResponse() + model_response: Final = ModelResponse() model_response.choices = [Choices(message=Message(content=""), index=0)] return OllamaChatConfig().transform_response( @@ -688,7 +688,7 @@ class TestOllamaStructuredErrorPayload: ) def test_streaming_error_chunk_surfaces_ollama_message(self): - iterator = OllamaChatCompletionResponseIterator(streaming_response=iter([]), sync_stream=True) + iterator: Final = OllamaChatCompletionResponseIterator(streaming_response=iter([]), sync_stream=True) with pytest.raises(OllamaError) as exc_info: iterator.chunk_parser({"error": OLLAMA_TOOL_CALL_PARSE_ERROR}) @@ -704,8 +704,8 @@ class TestOllamaStructuredErrorPayload: assert exc_info.value.status_code == 400 def test_streaming_error_chunk_does_not_mask_earlier_tool_call_state(self): - iterator = OllamaChatCompletionResponseIterator(streaming_response=iter([]), sync_stream=True) - tool_chunk = { + iterator: Final = OllamaChatCompletionResponseIterator(streaming_response=iter([]), sync_stream=True) + tool_chunk: Final = { "model": "gpt-oss:120b", "message": { "role": "assistant", @@ -722,7 +722,7 @@ class TestOllamaStructuredErrorPayload: assert exc_info.value.message == "llama runner process has terminated: exit status 2" def test_usage_uses_eval_count_when_message_has_no_content(self): - result = self._transform( + result: Final = self._transform( { "model": "gpt-oss:120b", "message": { From 4e9ea80c160b4e8737436b860ec9f53831c00bae Mon Sep 17 00:00:00 2001 From: Padma Date: Fri, 9 Oct 2026 12:11:42 +0000 Subject: [PATCH 3/3] fix(ollama_chat): drop no-op except to stay under the complexity limit chunk_parser was exactly at the C901 limit, so the new error check pushed it over. The except Exception: raise e block only re-raised the same error, so removing it keeps behavior the same and brings the function back to the limit. --- litellm/llms/ollama/chat/transformation.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/litellm/llms/ollama/chat/transformation.py b/litellm/llms/ollama/chat/transformation.py index 7b255611c12..8c7c8a1b269 100644 --- a/litellm/llms/ollama/chat/transformation.py +++ b/litellm/llms/ollama/chat/transformation.py @@ -567,5 +567,3 @@ class OllamaChatCompletionResponseIterator(BaseModelResponseIterator): status_code=400, headers={"Content-Type": "application/json"}, ) - except Exception as e: - raise e