OpenAI recently released structured outputs, which removes some of the complexity of using Instructor or other structured output libraries.
This pattern will transform your existing code to use the new structured outputs API.
Apply with the Grit CLI
grit apply instructor_to_openai
Sample Usage
Previously you would use Instructor to patch create
and add a response_format
arg.
BEFORE
import instructor from pydantic import BaseModel from openai import OpenAI # Define your desired output structure class UserInfo(BaseModel): name: str age: int client = instructor.from_openai(OpenAI()) # Extract structured data from natural language user_info = client.chat.completions.create( model="gpt-3.5-turbo", response_model=UserInfo, messages=[{"role": "user", "content": "John Doe is 30 years old."}], ) print(user_info.name) #> John Doe print(user_info.age) #> 30
Now you just need to use the parse
method directly on the OpenAI client.
AFTER
from pydantic import BaseModel from openai import OpenAI # Define your desired output structure class UserInfo(BaseModel): name: str age: int client = OpenAI() # Extract structured data from natural language user_info = client.beta.chat.completions.parse( model="gpt-3.5-turbo", response_format=UserInfo, messages=[{"role": "user", "content": "John Doe is 30 years old."}], ) if user_info.choices[0].message.refusal: raise Exception(f"GPT refused to comply! {user_info.choices[0].message.refusal}") user_info = user_info.choices[0].message.parsed print(user_info.name) #> John Doe print(user_info.age) #> 30