Skip to content

Conversation

@arjunprabhulal
Copy link

@arjunprabhulal arjunprabhulal commented Apr 13, 2025

Issue
fix(ollama): Handle non-tool-call JSON response when format=json
Issue occurs while using from google.adk.models.lite_llm import LiteLlm with llama3.2 , gemma3:27b

Error

venv/lib/python3.12/site-packages/litellm/llms/ollama/completion/transformation.py", line 266, in transform_response
    "name": function_call["name"],
            ~~~~~~~~~~~~~^^^^^^^^
KeyError: 'name'

Changes
This PR addresses a KeyError: 'name' that occurs in litellm/llms/ollama/completion/transformation.py when using certain Ollama models (e.g., ollama/llama3.2:latest, ollama/gemma3:27b) with tool calling enabled (format="json").
Problem:

When LiteLLM expects a tool call response, it requests format="json" from Ollama. However, some models return a valid JSON string in the response field, but the structure of this JSON does not match the expected tool call format ({"name": ..., "arguments": ...}). Instead, it might contain other structures (e.g., Schema.org JSON as seen with llama3.2). When transformation.py parses this JSON and attempts to access function_call["name"], it results in a KeyError.
adk-litellm-issue

@vercel
Copy link

vercel bot commented Apr 13, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
litellm ✅ Ready (Inspect) Visit Preview 💬 Add feedback Apr 14, 2025 0:46am
@arjunprabhulal
Copy link
Author

This PR addresses Infinite loop issue in ADK which couple of them have raised against the Function Calling using ADK. Although codebase directly uses google.adk.models.lite_llm.LiteLlm, and ADK module acts as a wrapper around the litellm library. When configured with an Ollama model (e.g., "ollama/..."), the ADK delegates the call to litellm, which internally uses litellm/llms/ollama/completion/transformation.py to process the Ollama API response. Therefore, changes within that specific litellm file can indirectly impact the behavior observed through the ADK when using Ollama models. So this PR merge is must to resolve the issue
image

@arjunprabhulal
Copy link
Author

Here is full code repo for above fix to work with external third party tools like with ADK - Function calling with Ollama/gemma3:27B and Ollama/llama3.2:latest and other models

https://github.com/arjunprabhulal/adk-gemma3-function-calling

@arjunprabhulal
Copy link
Author

Above code solves adk web

Before above code change
image

After above fix

image
@arjunprabhulal arjunprabhulal changed the title fix(ollama): Handle non-tool-call JSON response when format=json Apr 19, 2025
@CLAassistant
Copy link

CLAassistant commented Apr 22, 2025

CLA assistant check
All committers have signed the CLA.

@arjunprabhulal
Copy link
Author

arjunprabhulal commented Apr 22, 2025 via email

@BartzLeon
Copy link

Hey, are there any updates on when this is going to be merged?

@nileshtrivedi
Copy link

Any update on this PR? I think it's related to the open issue ollama/ollama#9941

@github-actions
Copy link

This pull request has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs.

@github-actions github-actions bot added the stale label Sep 30, 2025
@github-actions github-actions bot closed this Oct 8, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

4 participants