Skip to content

[Bug] The parse function in dspy's Adapters (ChatAdapter, JSONAdapter) can only partially validate output fields. #7925

@commiyou

Description

@commiyou

What happened?

The current implementation is as follows.

import pydantic

class MyModel(pydantic.BaseModel):
    model_config = pydantic.ConfigDict(validate_assignment=True)
    value:str = pydantic.Field(max_length=3)

fi = MyModel.model_fields["value"]

pydantic.TypeAdapter(fi.annotation).validate_python("1234")

With this approach, validating "1234" will not raise an error.

Image

The following approach can be used to validate with complete constraints.

getattr(MyModel.__pydantic_validator__.validate_assignment(MyModel.model_construct(), "value", "1234"),"value")
Image

The above approach is based on a Pydantic issue, where you can find more detailed discussions.

pydantic/pydantic#7367

Steps to reproduce

import pydantic

class MyModel(pydantic.BaseModel):
    model_config = pydantic.ConfigDict(validate_assignment=True)
    value:str = pydantic.Field(max_length=3)

fi = MyModel.model_fields["value"]

pydantic.TypeAdapter(fi.annotation).validate_python("1234")

DSPy version

2.6.10

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions