The OpenAI Python SDK provides several helper functions to simplify common tasks.
Convert a Pydantic model into a function tool for chat completions. This enables structured outputs with automatic schema generation.
Basic Usage
Function Signature
Parameters
Custom Name and Description
With Structured Outputs
Combine with the parse() method for automatic response parsing:
file_from_path()
Convert a file path to a format suitable for file upload APIs.
Basic Usage
Function Signature
Returns a tuple of (filename, contents) where:
filename is the base name of the file
contents is the file’s bytes
Example with Path Objects
Direct Path Usage
You can also pass Path objects directly without using file_from_path():
AssistantEventHandler
The Assistants API is deprecated. Use the Responses API instead.
Event handler for streaming Assistant responses. This provides structured access to events during Assistant runs.
Basic Usage
Available Methods
Override these methods to handle specific events:
Text Deltas Iterator
Access just the text deltas:
Properties
Access current state during streaming:
AsyncAssistantEventHandler
Async version of AssistantEventHandler:
Additional Utilities
Model Serialization
Convert Pydantic models to JSON:
Type Checking
The SDK provides full type hints for all methods and models: