Skip to content

The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]

Fix transformations crash when tools are being used

Bernardo Donadio63845952898c
diff --git a/litellm/llms/ollama/completion/transformation.py b/litellm/llms/ollama/completion/transformation.py
index 3565f090b04..4f7be507cc2 100644
--- a/litellm/llms/ollama/completion/transformation.py
+++ b/litellm/llms/ollama/completion/transformation.py
@@ -262,38 +262,52 @@ class OllamaConfig(BaseConfig):
## RESPONSE OBJECT
model_response.choices[0].finish_reason = "stop"
if request_data.get("format", "") == "json":
- response_content = json.loads(response_json["response"])
-
- # Check if this is a function call format with name/arguments structure
- if (
- isinstance(response_content, dict)
- and "name" in response_content
- and "arguments" in response_content
- ):
- # Handle as function call (original behavior)
- function_call = response_content
- message = litellm.Message(
- content=None,
- tool_calls=[
- {
- "id": f"call_{str(uuid.uuid4())}",
- "function": {
- "name": function_call["name"],
- "arguments": json.dumps(function_call["arguments"]),
- },
- "type": "function",
- }
- ],
- )
- model_response.choices[0].message = message # type: ignore
- model_response.choices[0].finish_reason = "tool_calls"
- else:
- # Handle as regular JSON (new behavior)
- message = litellm.Message(
- content=json.dumps(response_content),
- )
+ # Check if response field exists and is not empty before parsing JSON
+ response_text = response_json.get("response", "")
+ if not response_text or not response_text.strip():
+ # Handle empty response gracefully - set empty content
+ message = litellm.Message(content="")
model_response.choices[0].message = message # type: ignore
model_response.choices[0].finish_reason = "stop"
+ else:
+ try:
+ response_content = json.loads(response_text)
+
+ # Check if this is a function call format with name/arguments structure
+ if (
+ isinstance(response_content, dict)
+ and "name" in response_content
+ and "arguments" in response_content
+ ):
+ # Handle as function call (original behavior)
+ function_call = response_content
+ message = litellm.Message(
+ content=None,
+ tool_calls=[
+ {
+ "id": f"call_{str(uuid.uuid4())}",
+ "function": {
+ "name": function_call["name"],
+ "arguments": json.dumps(function_call["arguments"]),
+ },
+ "type": "function",
+ }
+ ],
+ )
+ model_response.choices[0].message = message # type: ignore
+ model_response.choices[0].finish_reason = "tool_calls"
+ else:
+ # Handle as regular JSON (new behavior)
+ message = litellm.Message(
+ content=json.dumps(response_content),
+ )
+ model_response.choices[0].message = message # type: ignore
+ model_response.choices[0].finish_reason = "stop"
+ except json.JSONDecodeError:
+ # If JSON parsing fails, treat as regular text response
+ message = litellm.Message(content=response_text)
+ model_response.choices[0].message = message # type: ignore
+ model_response.choices[0].finish_reason = "stop"
else:
model_response.choices[0].message.content = response_json["response"] # type: ignore
model_response.created = int(time.time())