Skip to content

Gemini specific Modes do not work with LiteLLM #1489

@JohanBekker

Description

@JohanBekker
  • This is actually a bug report.
  • I am not getting good LLM Results
  • I have tried asking for help in the community on discord or discussions and have not received a response.
  • I have tried searching the documentation and have not found an answer.

What Model are you using?

  • gpt-3.5-turbo
  • gpt-4-turbo
  • gpt-4
  • Other (please specify)

Describe the bug
When using .from_litellm to call LLM's through LiteLLM, an error is raised when calling the LLM.

To Reproduce
Example:

from pydantic import BaseModel, Field
from litellm import Router
from dotenv import load_dotenv
import instructor

load_dotenv() # Gemini API key


router = Router(
    model_list=[
        {
            "model_name": "gemini-2.0-flash",
            "litellm_params": {
                "model": "gemini/gemini-2.0-flash-001",
            },
        },
        {
                "model_name": "gemini-2.5-flash",
                "litellm_params": {
                    "model": "gemini/gemini-2.5-flash-preview-04-17",
                },
            },
    ],
    cache_responses=False,
    set_verbose=True,
    debug_level="DEBUG",
)

class PersonalInfo(BaseModel):
    name: str = Field(..., description="The person's name.")
    age: int = Field(..., description="The person")
    addres: str = Field("unknown", description="The person's address.") # Default fields other than None do not work for gemini
    preoccupation: str | None = Field(None, description="The person's occupation.")

messages = [
    {
        "role": "system",
        "content": "You are a helpful assistant specialized in extracting data in structured JSON formats.."
    },
    {
        "role": "user",
        "content": "John Doe is 87 years old."
    },
]

llm = instructor.from_litellm(
    router.completion,
    mode=instructor.Mode.GEMINI_JSON,
)

response: PersonalInfo = await llm.chat.completions.create(
    model="gemini-2.0-flash",
    response_model=PersonalInfo,
    messages=messages,
)

Error message:

AssertionError: Gemini `model` must be set while patching the client, not passed as a parameter to the create method

Expected behavior
Would be great if it works with Gemini through LiteLLM so I can use Gemini specific modes while keeping load balancing, fallbacks of LiteLLM.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions