This commit is contained in:
Padma-Deepak 2026-11-05 07:34:09 +00:00 • committed by GitHub
commit 9b67dd579e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 100 additions and 5 deletions

View file

@ -341,6 +341,8 @@ class OllamaChatConfig(BaseConfig):
)
response_json: Final = raw_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")
@ -393,9 +395,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,
@ -424,6 +427,14 @@ class OllamaChatConfig(BaseConfig):
)
def _structured_error(message: str) -> OllamaError:
return OllamaError(
message=message,
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")
@ -482,6 +493,9 @@ class OllamaChatCompletionResponseIterator(BaseModelResponseIterator):
"""
from litellm.types.utils import Delta, StreamingChoices
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")
if tool_calls is not None:
@ -558,5 +572,3 @@ class OllamaChatCompletionResponseIterator(BaseModelResponseIterator):
status_code=400,
headers={"Content-Type": "application/json"},
)
except Exception as e:
raise e

View file

@ -18,6 +18,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
@ -661,6 +662,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: 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[str, object]) -> ModelResponse:
mock_response: Final = MagicMock()
mock_response.json.return_value = ollama_response
mock_response.text = json.dumps(ollama_response)
model_response: Final = 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: Final = 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: Final = OllamaChatCompletionResponseIterator(streaming_response=iter([]), sync_stream=True)
tool_chunk: Final = {
"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: Final = 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."""