Hello I am looking for guidance on how I could go about using the asynchronous client with structured outputs. Within the scope of my current project I have a bunch of sequential API calls that utilize structured outputs, to reduce latency I figured that I would run these calls asynchronously. Unfortunately, I cannot seem to get it to work.
I cannot show the exact code I am working with due to project rules, but using an example this is what I am trying to accomplish:
from pydantic import BaseModel
from openai import AsyncOpenAI
import asyncio
async_client = AsyncOpenAI()
class CalendarEvent(BaseModel):
name: str
date: str
participants: list[str]
async def asyncExample():
async_client = AsyncOpenAI()
completion = await async_client.chat.completions.parse(
model="gpt-4o-2024-08-06",
messages=[
{"role": "system", "content": "Extract the event information."},
{"role": "user", "content": "Alice and Bob are going to a science fair on Friday."},
],
response_format=CalendarEvent,
)
event = completion.choices[0].message.parsed
return event
However, this has been throwing me an error saying:
AttributeError: ‘AsyncCompletions’ object has no attribute ‘parse’.
Since the interface I am creating requires multiple structured output calls in a row (that do not rely upon each other), having them run asynchronously or at the same time in some capacity would do me wonders in reducing latency. As such, I was wondering if anyone knows of a way to accomplish this?