Replies: 1 comment 2 replies
-
from openai import OpenAI
import instructor
from pydantic import BaseModel, Field, field_validator, AfterValidator
from typing import List, Optional
from typing_extensions import Annotated
# Apply the patch to the OpenAI client
client = instructor.patch(OpenAI())
def validate_name(v):
if v.upper() != v:
print("validation error was found")
raise ValueError("Name must be in uppercase.")
return v
class UserDetails(BaseModel):
name: Annotated[str, AfterValidator(validate_name)]
age: int
MaybeUserDetails = instructor.Maybe(UserDetails)
model = client.chat.completions.create(
model="gpt-3.5-turbo",
response_model=MaybeUserDetails,
max_retries=10,
messages=[
{"role": "user", "content": "Extract jason is 25 years old"},
],
)
assert model.result.name == "JASON" Try running this, you'll find that the
|
Beta Was this translation helpful? Give feedback.
2 replies
Answer selected by
jxnl
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
I'm curious what happens in the following example:
Let's say there's an ValidationError that occurs, does it bubble up to Retries or does it just return a Maybe object with the Error attribute populated?
Beta Was this translation helpful? Give feedback.
All reactions