You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
# Direct litellm completion approachfrompydanticimportBaseModelfromlitellmimportcompletionclassPerson(BaseModel):
name: strage: intoccupation: strhobbies: list[str]
response=completion(
model="anthropic/claude-3-5-sonnet-20240620",
messages=[
{
"role": "system",
"content": "You are a helpful assistant that generates information about a person.",
},
{"role": "user", "content": "Generate a person"},
],
response_format=Person, # Pass the Pydantic model directly
)
And in response I got the following error message:
During handling of the above exception, another exception occurred:
BadRequestError Traceback (most recent call last)
Cell In[5], line 13
9 occupation: str
10 hobbies: list[str]
---> 13 response = completion(
14 model="anthropic/claude-3-sonnet-20240320",
15 messages=[
16 {
17 "role": "system",
18 "content": "You are a helpful assistant that generates information about a person.",
19 },
20 {"role": "user", "content": "Generate a person"},
21 ],
22 response_format=Person, # Pass the Pydantic model directly
23 )
File ~/github/software/llamabot/.pixi/envs/notebooks/lib/python3.12/site-packages/litellm/utils.py:959, in client..wrapper(*args, **kwargs)
955 if logging_obj:
956 logging_obj.failure_handler(
957 e, traceback_exception, start_time, end_time
958 ) # DO NOT MAKE THREADED - router retry fallback relies on this!
--> 959 raise e
File ~/github/software/llamabot/.pixi/envs/notebooks/lib/python3.12/site-packages/litellm/utils.py:848, in client..wrapper(*args, **kwargs)
846 print_verbose(f"Error while checking max token limit: {str(e)}")
847 # MODEL CALL
--> 848 result = original_function(*args, **kwargs)
849 end_time = datetime.datetime.now()
850 if "stream" in kwargs and kwargs["stream"] is True:
BadRequestError: litellm.BadRequestError: AnthropicException - {"type":"error","error":{"type":"invalid_request_error","message":"tools.0.input_schema: JSON schema is invalid - please consult https://json-schema.org or our documentation at https://docs.anthropic.com/en/docs/tool-use\"}}"
}
On the other hand, when I do a drop-in replacement of the model name with got-4o, everything runs perfectly fine:
response=completion(
model="gpt-4o",
messages=[
{
"role": "system",
"content": "You are a helpful assistant that generates information about a person.",
},
{"role": "user", "content": "Generate a person"},
],
response_format=Person, # Pass the Pydantic model directly
)
It appears that to solve the problem with anthropic models, I actually need to do the model JSON schema dump and pass that into the response format keyword argument:
response=completion(
model="anthropic/claude-3-5-sonnet-20240620",
messages=[
{
"role": "system",
"content": "You are a helpful assistant that generates information about a person.",
},
{"role": "user", "content": "Generate a person"},
],
response_format=Person.model_json_schema(),
)
I believe it should be better that the two behaviors are harmonized and actually made consistent. In case it is helpful, my opinion is that the first format is better. There's fewer things for us to worry about. We should just use be passing in the object rather than the model JSON schema.
The text was updated successfully, but these errors were encountered:
ericmjl
changed the title
Inconsistent behavior when passing in Pytantic models for structured generation
Inconsistent behavior when passing in Pydantic models for structured generation
Jan 5, 2025
Hmmm, I suspect something changed between version 1.3x.y and 1.5x.y I recently just upgraded to 1.56.10 and the error goes away. I apologize for not first checking package versions, should have done that first!
UPDATE: I have a GitHub gist to illustrate the issue: https://gist.github.com/ericmjl/f50e1cc716e566770ead755c9c1e28a3
I recently tried the following code:
And in response I got the following error message:
On the other hand, when I do a drop-in replacement of the model name with
got-4o
, everything runs perfectly fine:It appears that to solve the problem with anthropic models, I actually need to do the model JSON schema dump and pass that into the response format keyword argument:
I believe it should be better that the two behaviors are harmonized and actually made consistent. In case it is helpful, my opinion is that the first format is better. There's fewer things for us to worry about. We should just use be passing in the object rather than the model JSON schema.
The text was updated successfully, but these errors were encountered: