Skip to content

How does Maybe work with Retries? #208

Answered by jxnl
samching asked this question in Q&A
Discussion options

You must be logged in to vote
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,…

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@samching
Comment options

@jxnl
Comment options

Answer selected by jxnl
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants