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

fix: handle nested pydantic model args in schema generation #2439

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

lemorage
Copy link
Contributor

Please describe the purpose of this pull request.
This PR fixes the issue that occurs when nested pedantic model input is used as an argument. See the test section for an example.

How to test

class Steps(BaseModel):
steps: list[Step] = Field(
...,
description="A list of steps to add to the task plan.",
)

Change the steps variable annotation to Step, and then run pydantic_model_to_json_schema(Steps) without this fix, we would get the following error:

>>> pydantic_model_to_json_schema(Steps)
Traceback (most recent call last):
  File "<python-input-16>", line 1, in <module>
    pydantic_model_to_json_schema(Steps)
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^
  File "/Users/clannad/Dev/GitHub/letta/letta/functions/schema_generator.py", line 301, in pydantic_model_to_json_schema
    return clean_schema(schema_part=schema, full_schema=schema)
  File "/Users/clannad/Dev/GitHub/letta/letta/functions/schema_generator.py", line 286, in clean_schema
    properties[name] = clean_property(prop)
                       ~~~~~~~~~~~~~~^^^^^^
  File "/Users/clannad/Dev/GitHub/letta/letta/functions/schema_generator.py", line 239, in clean_property
    "type": "string" if prop["type"] == "string" else prop["type"],
                        ~~~~^^^^^^^^
KeyError: 'type'

After this fix:

>>> pydantic_model_to_json_schema(Steps)
{
  "type": "object",
  "properties": {
    "steps": {
      "type": "object",
      "properties": {
        "name": {
          "type": "string",
          "description": "Name of the step."
        },
        "key": {
          "type": "string",
          "description": "Unique identifier for the step."
        },
        "description": {
          "type": "string",
          "description": "An exhaustic description of what this step is trying to achieve and accomplish."
        }
      },
      "required": ["name", "key", "description"],
      "description": "A list of steps to add to the task plan."
    }
  },
  "required": ["steps"]
}

Related issues or PRs
fixes #2403

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.

Schema Generation Fails with 'type' Error When Using Nested Pydantic Models
1 participant