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

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

Closed
ADR-007 opened this issue Apr 24, 2024 · 7 comments
Closed

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

ADR-007 opened this issue Apr 24, 2024 · 7 comments
Labels
enhancement New feature or request

Comments

@ADR-007
Copy link

ADR-007 commented Apr 24, 2024

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)
@dosubot dosubot bot added the enhancement New feature or request label Apr 24, 2024
@jxnl
Copy link
Owner

jxnl commented Apr 24, 2024

Happy to take or for this in process response.

@ivanleomk
Copy link
Collaborator

@ADR-007 just pushed up a PR which introduces this. Is this what you had in mind for your use case?

from typing_extensions import TypedDict
from openai import OpenAI
import instructor


class User(TypedDict):
    name: str
    age: int


client = instructor.from_openai(OpenAI())

print(
    client.chat.completions.create(
        model="gpt-3.5-turbo",
        response_model=User,
        messages=[
            {
                "role": "user",
                "content": "Timothy is a man from New York who is turning 32 this year",
            }
        ],
    )
)

"""
name='Timothy' age=32
"""

@ADR-007
Copy link
Author

ADR-007 commented Jun 13, 2024

@ivanleomk yes, thank you!

@ADR-007 ADR-007 closed this as completed Jun 13, 2024
@ADR-007
Copy link
Author

ADR-007 commented Jun 13, 2024

Oh. That PR is not yet merged, so I should leave this issue open

@ADR-007 ADR-007 reopened this Jun 13, 2024
@ADR-007
Copy link
Author

ADR-007 commented Jun 13, 2024

I would really like to have this implemented. It is currently the only thing that blocks me from using this library :(

@jxnl
Copy link
Owner

jxnl commented Jun 16, 2024

we're close!

@ivanleomk
Copy link
Collaborator

@ADR-007 PR #758 has been merged so going to close this issue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants