Skip to content
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

(Core) tool_choice overiding for with_structured_output for BaseChatModel #29321

Closed

Conversation

keenborder786
Copy link
Contributor

Basically for above issue, with this change the user will be able to do the following:

import os
from langchain_community.chat_models import ChatLiteLLM
from langchain_google_genai import ChatGoogleGenerativeAI

from typing import Optional

from pydantic import BaseModel, Field


class Joke(BaseModel):
    """Joke to tell user."""

    setup: str = Field(description="The setup of the joke")
    punchline: str = Field(description="The punchline to the joke")
    rating: Optional[int] = Field(
        default=None, description="How funny the joke is, from 1 to 10"
    )


llm_litellm = ChatLiteLLM(
    model="gemini/gemini-2.0-flash-exp",
    api_key=os.getenv("API_KEY"),
)

stuctured_litellm = llm_litellm.with_structured_output(Joke, include_raw=True, tool_choice="any")

try:
    response_litellm = stuctured_litellm.invoke("Tell me a joke about cats")
    print(response_litellm)
except Exception as e:
    print(f"An error occured for litellm: {str(e)}")

Copy link

vercel bot commented Jan 21, 2025

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

1 Skipped Deployment
Name Status Preview Comments Updated (UTC)
langchain ⬜️ Ignored (Inspect) Visit Preview Jan 25, 2025 9:41pm

@dosubot dosubot bot added size:XS This PR changes 0-9 lines, ignoring generated files. 🤖:nit Small modifications/deletions, fixes, deps or improvements to existing code or docs labels Jan 21, 2025
@keenborder786
Copy link
Contributor Author

@ccurme

Copy link
Collaborator

@ccurme ccurme left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure we want to put it on the user to know what value of tool_choice to use, since for structured output we know we want to force structure according to the (one) given schema.

ChateLiteLLM.bind_tools should be handling the tool_choice="any" case. It has some conditions there for Azure, does doing something similar for Gemini fix things?

@ccurme ccurme self-assigned this Jan 23, 2025
@keenborder786
Copy link
Contributor Author

@ccurme I am not so sure but will check

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🤖:nit Small modifications/deletions, fixes, deps or improvements to existing code or docs size:XS This PR changes 0-9 lines, ignoring generated files.
Projects
Status: Closed
Development

Successfully merging this pull request may close these issues.

2 participants