-
Notifications
You must be signed in to change notification settings - Fork 858
When using LiteLlm with a model that can produce structured output (e.g. gpt-4o), adk doesn't seem to be passing the output schema to the model #217
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
What errors are you seeing? Does the above agent definition reproduce the error? |
Yes the above agent definition reproduces the error every single time. Below is what I am seeing on the terminal. Note that the response from open ai is not a Json string, and pydantic is throwing a validation error because of it. The fact that openai's response is plain text is making me think that the underlying API call isn't asking for structured output and passing the schema for the Pydantic models. It works as expected when I use a gemini model directly. This is only a problem when I use LiteLlm. Summary of what I am observing:
|
I've had somewhat the same issue: the object is not returned but a JSON string. I get this error with gemini-flash-002. |
Same here, LiteLLM with Ollama not passing the json schema correctly which causes invalid response. This is the main reason why I'm moving away from adk even though it seemed the most complete framework and I've tried them all, literally 😢 |
My workaround for this is to pass the schema directly into LiteLlm: routing_agent = LlmAgent(
name="routing_agent",
model=LiteLlm(
api_base=setting.OLLAMA_API_BASE,
model="ollama_chat" + "/" + setting.ROUTING_MODEL,
format=RoutingSchema.model_json_schema(),
),
instruction=ROUTING_INSTRUCTION,
output_schema=RoutingSchema,
output_key="routing_agent",
disallow_transfer_to_parent=True,
disallow_transfer_to_peers=True,
) I define both the output schema and the format (in my case, the variable name is Args:
model: The name of the LiteLlm model.
**kwargs: Additional arguments to pass to the litellm completion api. This means I can pass extra parameters—just like when calling the API manually—through ADK is still a relatively new framework, so bugs like this are somewhat expected. However, output schema handling is a pretty fundamental feature, so I hope they will fix this issue soon. |
@trungpq27 nice workaround, thanks.
'[intent_recognition_agent] said: { "type": "Booking a space" }' it looks like agent identifier prefix is added to the text and because of it, it can't be json decoded |
This does not apply if NOT using LiteLlm and other models than Gemini. The issue exists even for Gemini. |
Can you provide the context in which you encountered the issue? Is this an agent or a sub-agent? |
Here is a workaround that works:
|
I'm primarily using Ollama (for local) and Bedrock (for remote) as my LLM providers and the following works in both cases: import os
from typing import Type, TypeVar
from google.adk.models.lite_llm import LiteLlm
from pydantic import BaseModel
PROIVDER = "bedrock" # "ollama" | "bedrock"
if PROIVDER == "bedrock":
os.environ["AWS_REGION_NAME"] = "us-east-1"
T = TypeVar("T", bound=BaseModel)
def get_model(output_model: Type[T] | None = None):
kwargs = {}
if PROIVDER == "bedrock":
kwargs["model"] = "bedrock/us.anthropic.claude-3-5-haiku-20241022-v1:0"
if output_model is not None:
kwargs["response_format"] = output_model
else:
kwargs["model"] = "ollama_chat/qwen2.5:3b"
kwargs["api_base"] = "http://localhost:11434"
if output_model is not None:
kwargs["format"] = output_model.model_json_schema()
return LiteLlm(
**kwargs,
) Then for the agent init I use this model getter and |
Fixed in #580 |
@hangfei When the LiteLlm call is made with |
I have a very basic agent that is using openai/gpt-4o. I am trying to get structured output from it. However, the model either doesn't return JSON or it returns a json response that doesn't follow the given schema.
I am not observing this if I use a model provided by google such as gemini-2.0-flash.
Here is a sample code to reproduce the issue:
When the response is received, Pydantic throws a validation error.
The text was updated successfully, but these errors were encountered: