From 63845952898cf7523459e5f59fed468565401ac4 Mon Sep 17 00:00:00 2001 From: Bernardo Donadio Date: Fri, 22 Aug 2025 14:48:28 -0300 Subject: [PATCH] Fix transformations crash when tools are being used --- .../llms/ollama/completion/transformation.py | 74 +++++++++++-------- 1 file changed, 44 insertions(+), 30 deletions(-) diff --git a/litellm/llms/ollama/completion/transformation.py b/litellm/llms/ollama/completion/transformation.py index 3565f090b0..4f7be507cc 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())