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

Add Structured Outputs support to Assistants stream() and create_and_poll() Functions #1698

Open
1 task done
sciencetor2 opened this issue Sep 9, 2024 · 0 comments
Open
1 task done
Labels
enhancement New feature or request

Comments

@sciencetor2
Copy link

sciencetor2 commented Sep 9, 2024

Confirm this is a feature request for the Python library and not the underlying OpenAI API.

  • This is a feature request for the Python library

Describe the feature or improvement you're requesting

Currently the client.beta.threads.runs.create_and_poll() function and client.beta.threads.runs.stream() function do not accept a pydantic model as their "response_format". currently they only accept the old {"type": "json_object"} value.

Additional context

class Meal(BaseModel):
    meal: str
    slug: str
    recipe_id: str
    calories_per_serving: int
    protein_per_serving: int
    fat_per_serving: int
    carbs_per_serving: int
    servings: int

class Meals(BaseModel):
    breakfast: Optional[Meal]
    lunch: Optional[Meal]
    dinner: Optional[Meal]

class DayLog(BaseModel):
    date: str  # You can change this to 'date' type if needed
    total_calories: int
    total_carbs: int
    total_fat: int
    total_protein: int
    meals: Meals

class WeekLog(BaseModel):
    Monday: DayLog
    Tuesday: DayLog
    Wednesday: DayLog
    Thursday: DayLog
    Friday: DayLog
    Saturday: DayLog
    Sunday: DayLog

completion = client.beta.chat.completions.parse(
        model="gpt-4o-2024-08-06",
        messages=[
            {"role": "system", "content": "my prompt for structured data"


             },
        ],
        response_format=WeekLog,
    )

Currently the above works without issue, but the below throws a TypeError:

assistant = client.beta.assistants.create(
        name="Meal Planner Nutritionist",
        instructions="some instructions",
        tools=[{"type": "code_interpreter"}],
        model="gpt-4o-2024-08-06",
    )
    thread = client.beta.threads.create()
    message = client.beta.threads.messages.create(
        thread_id=thread.id,
        role="user",
        content= "my prompt for structured data"
         )
    run = client.beta.threads.runs.create_and_poll(
        thread_id=thread.id,
        assistant_id=assistant.id,
        instructions="repeat instructions",
        response_format=WeekLog
    )

and the below works, but isnt usable for my purposes:

assistant = client.beta.assistants.create(
        name="Meal Planner Nutritionist",
        instructions="some instructions",
        tools=[{"type": "code_interpreter"}],
        model="gpt-4o-2024-08-06",
    )
    thread = client.beta.threads.create()
    message = client.beta.threads.messages.create(
        thread_id=thread.id,
        role="user",
        content= "my prompt for structured data"
         )
    run = client.beta.threads.runs.create_and_poll(
        thread_id=thread.id,
        assistant_id=assistant.id,
        instructions="repeat instructions",
        response_format={"type": "json_object"}
    )
@rattrayalex rattrayalex added the enhancement New feature or request label Sep 9, 2024
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

2 participants