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

[Bug]: json mode with nested models doesn't work for Anthropic #7755

Closed
amanpreet692 opened this issue Jan 14, 2025 · 5 comments · Fixed by #7844
Closed

[Bug]: json mode with nested models doesn't work for Anthropic #7755

amanpreet692 opened this issue Jan 14, 2025 · 5 comments · Fixed by #7844
Assignees
Labels
bug Something isn't working mlops user request

Comments

@amanpreet692
Copy link

What happened?

A bug happened!
Picking the example from your docs @ https://docs.litellm.ai/docs/completion/json_mode:

import os
from litellm import completion 
from pydantic import BaseModel

# add to env var 
os.environ["OPENAI_API_KEY"] = ""

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]

resp = completion(
    model="gpt-4o-2024-08-06",
    messages=messages,
    response_format=EventsList
)

print("Received={}".format(resp))

The above won't work if you replace model with anthropic/claude-3-5-sonnet-20241022 and add the anthropic key.

Relevant log output

**Error** 

``litellm.exceptions.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"}}``

Are you a ML Ops Team?

Yes

What LiteLLM version are you on ?

v1.53.1 - v1.58.0

Twitter / LinkedIn details

No response

@amanpreet692 amanpreet692 added the bug Something isn't working label Jan 14, 2025
@sava-t
Copy link

sava-t commented Jan 14, 2025

Yes! I've been having similar problems.

@krrishdholakia krrishdholakia self-assigned this Jan 15, 2025
@krrishdholakia
Copy link
Contributor

able to repro

@krrishdholakia
Copy link
Contributor

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 litellm._turn_on_debug()

Any help analyzing this further would be great! (e.g. how should the schema for anthropic be handled differently)

relevant code - anthropic -

_input_schema["properties"] = {"values": json_schema}

convert pydantic to json schema -

non_default_params["response_format"] = type_to_response_format_param(

@amanpreet692
Copy link
Author

Thanks for the pointers @krrishdholakia !
I tried a few things and this snippet works:

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.
It could potentially be because the json schema is first processed through the open ai _pydantic.py script which might not be compatible with anthropic but I can't be sure about this.

Hope this was helpful!

@krrishdholakia
Copy link
Contributor

Amazing, thank you for the find. I'll push a fix today. @amanpreet692

krrishdholakia added a commit that referenced this issue Jan 18, 2025
… + 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
@krrishdholakia krrishdholakia marked this as a duplicate of #7696 Jan 18, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working mlops user request
Projects
None yet
3 participants