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

Add pydantic support in response_format #2647

Open
wants to merge 7 commits into
base: main
Choose a base branch
from

Conversation

lhoestq
Copy link
Member

@lhoestq lhoestq commented Oct 31, 2024

  • support passing a Pydantic schema in response_format in InferenceClient.chat_completion
class MyExampleResponseFormat(BaseModel):
    foo: str
    bar: str

response = client.chat_completion(messages=messages, response_format=MyExampleResponseFormat)
  • added response.choices[0].message.parsed (and response.choices[0].message.refusal if the generation failed as in the openai client)
message = response.choices[0].message
if message.parsed:
    print(message.parsed)
else:
    print(message.refusal)

close #2646

@HuggingFaceDocBuilderDev

The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.

@Wauplin
Copy link
Contributor

Wauplin commented Oct 31, 2024

As discussed, we'll have to see what we can do for this. Especially because ChatCompletionOutputMessage is an auto-generated class (also the stream version ChatCompletionStreamOutputMessage would have to be updated). And do the same for InferenceClient.text_generation(..., grammar=...).

@lhoestq
Copy link
Member Author

lhoestq commented Oct 31, 2024

Ok, note that this is mostly useful for the non-streaming case imo

@patrickvonplaten
Copy link
Contributor

patrickvonplaten commented Feb 27, 2025

Big +1 on adding pydantic to both requests and responses. If HF becomes the gateway for many inference providers, it'd be super important to make the user experience with it as flawless as possible. To do so, I think both requests and responses objects should have very strict typing and validation that throw directly once the user makes a mistake => Pydantic is a must for this.

E.g.:

Strict validation would also be very important here - otherwise people might waste quite some money on ill-formatted requests

@Wauplin
Copy link
Contributor

Wauplin commented Feb 27, 2025

@patrickvonplaten could you open a separate issue for that topic please? @lhoestq's initial request is simply that the response_format parameter from chat_completion accepts Pydantic model as well, not only a jsonschema.
Adding validation to all inputs and outputs is an orthogonal (and big!) topic so I'd prefer to have the discussion in a dedicated issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

InferenceClient: allow passing a pydantic model as response_format
4 participants