instructor icon indicating copy to clipboard operation
instructor copied to clipboard

Any valid Pydantic type should be supported (e.g. TypedDict)

Open ADR-007 opened this issue 10 months ago • 1 comments

Is your feature request related to a problem? Please describe. Currently, only Pydantic models are supported as response_model. But in some cases, I want to use TypedDict instead. For example, I don't want to do massive refactoring, so I just want to add response schema validation to an existing job.

Describe the solution you'd like I would like to able to use TypedDict as response_model. E.g.:

class MyModel(TypedDict):
    my_key: str

result = client.chat.completions.create(
    messages=[...],
    response_model=MyModel,
)

It is very simple to implement in Pydantic V2:

from pydantic import TypeAdapter

adapter = TypeAdapter(MyModel)
json_schema = adapter.json_schema()

result = prcess_llm(prompt, json_schema, ...)

my_model = adapter.validate_python(result)

ADR-007 avatar Apr 24 '24 15:04 ADR-007