Skip to content

Add pydantic field descriptions in prompts - #14

Open
banana-bison wants to merge 2 commits into
swe-productivity:mainfrom
banana-bison:pydantic
Open

banana-bison wants to merge 2 commits into
swe-productivity:mainfrom
banana-bison:pydantic

Conversation

@banana-bison

@banana-bison banana-bison commented Dec 27, 2025

Copy link
Copy Markdown

Fixes: #5

In case of a pydantic description such as this:

class Address(BaseModel):
      street: str = Field(description="Street address")
      city: str = Field(description="City name")
      zip_code: str = Field(description="ZIP or postal code")

  class Person(BaseModel):
      name: str = Field(description="Full name of the person")
      age: int = Field(description="Age in years")
      email: str = Field(description="Email address")
      address: Address = Field(description="Home address")

The LLM would see a description like this:

1. `person` (Person): Person to process
      Fields:
      - name (str): Full name of the person
      - age (int): Age in years
      - email (str): Email address
      - address (Address): Home address
          - street (str): Street address
          - city (str): City name
          - zip_code (str): ZIP or postal code

Earlier it could see just person, not the nested fields that it contains.

Signed-off-by: banana-bison <bananabison@gmail.com>
Comment thread dspy/adapters/utils.py Outdated
Comment thread dspy/adapters/utils.py Outdated
Comment thread dspy/adapters/utils.py
Comment thread dspy/adapters/utils.py Outdated
def extract_pydantic_field_descriptions(
model_class: type,
max_depth: int = 3,
_visited: set | None = None,

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's remove _

Comment thread dspy/adapters/utils.py Outdated
Comment thread dspy/adapters/utils.py Outdated
Returns:
Formatted string with field descriptions
"""
from pydantic import BaseModel

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's move to top

Comment thread dspy/adapters/utils.py Outdated
Comment thread dspy/adapters/utils.py Outdated
Comment thread dspy/adapters/utils.py
args = get_args(annotation)
return any(is_pydantic_model(arg) for arg in args)

except (TypeError, ImportError):

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Which part do we expect to fail?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issubclass could raise an exception for inputs such as issubclass(Any, BaseModel). But since this is an internal function, I guess this is a little too defensive. I can remove it.

Comment thread dspy/adapters/utils.py
Comment thread dspy/adapters/utils.py Outdated
Comment thread tests/adapters/test_pydantic_field_descriptions.py
@TomeHirata

TomeHirata commented Mar 8, 2026

Copy link
Copy Markdown
Collaborator

Can we describe what the current prompt looks like and what it should be in the PR description?

Signed-off-by: banana-bison <bananabison@gmail.com>
@banana-bison

Copy link
Copy Markdown
Author

@TomeHirata i've address all the comments and updated the PR description

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.

[Feature] Include Pydantic Field Descriptions in Prompts

2 participants