Skip to content

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

Open
@sciencetor2

Description

@sciencetor2

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"}
    )

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions