-
-
Notifications
You must be signed in to change notification settings - Fork 2k
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
[Bug]: json mode with nested models doesn't work for Anthropic #7755
Comments
Yes! I've been having similar problems. |
able to repro |
So i can see a valid json schema being generated, but anthropic seems to reject it. Will need to investigate further. You can see the raw request to anthropic with Any help analyzing this further would be great! (e.g. how should the schema for anthropic be handled differently) relevant code - anthropic -
convert pydantic to json schema - Line 2777 in df7d500
|
Thanks for the pointers @krrishdholakia ! messages = [{"role": "user", "content": "List 5 important events in the XIX century"}]
class CalendarEvent(BaseModel):
name: str
date: str
participants: list[str]
class EventsList(BaseModel):
events: list[CalendarEvent]
schema = EventsList.model_json_schema(ref_template="/$defs/{model}")
json_schema = {
"type": "json_schema",
"json_schema": {
"schema": schema,
"name": EventsList.__name__,
"strict": True,
}
}
resp = litellm.completion(
model="anthropic/claude-3-5-sonnet-20241022",
messages=messages,
response_format=json_schema)
res_str = resp["choices"][0]["message"]["content"].strip()
print(json.loads(res_str)) Somehow Anthropic has an issue with the default ref template of "#/$defs/{model}" . It doesn't process the pound sign and errors out while processing the refs in case of nested jsons. Hope this was helpful! |
Amazing, thank you for the find. I'll push a fix today. @amanpreet692 |
… + Anthropic nested pydantic object support (#7844) * build: ensure all regional bedrock models have same supported values as base bedrock model prevents drift * test(base_llm_unit_tests.py): add testing for nested pydantic objects * fix(test_utils.py): add test_get_potential_model_names * fix(anthropic/chat/transformation.py): support nested pydantic objects Fixes #7755
What happened?
A bug happened!
Picking the example from your docs @ https://docs.litellm.ai/docs/completion/json_mode:
The above won't work if you replace model with
anthropic/claude-3-5-sonnet-20241022
and add the anthropic key.Relevant log output
Are you a ML Ops Team?
Yes
What LiteLLM version are you on ?
v1.53.1 - v1.58.0
Twitter / LinkedIn details
No response
The text was updated successfully, but these errors were encountered: