Skip to main content

Overview

The OpenAI provider offers comprehensive support for all OpenAI services including GPT models, embeddings, function calling, and structured output. It’s the most feature-complete provider in the SDK.

Quick Start

Available Models

Chat Models

| gpt-4o-mini | Latest GPT-4 model | 128k | 5/1Minput,5/1M input, 15/1M output |

Embedding Models

Basic Usage

Text Generation

Streaming

With System Instructions

Advanced Features

Structured Output

OpenAI supports native structured output with response_format="json_object":

Function Calling

Embeddings

Vision Models

Configuration

API Key

Set your OpenAI API key:
Or pass it directly:

Default Parameters

Configure default parameters for all requests:

Organization

For team accounts:

Parameters

Common Parameters

Generation Parameters

Embedding Parameters

Error Handling

Rate Limiting

Token Limits

Best Practices

1. Model Selection

Choose the right model for your use case:

2. Cost Optimization

Monitor and optimize costs:

3. Prompt Engineering

Use clear, specific prompts:

4. Streaming for Long Responses

Use streaming for better user experience:

5. Structured Output for Reliability

Use structured output for consistent results:

Troubleshooting

Common Issues

  1. Invalid API Key
    • Check your API key is correct
    • Ensure you have sufficient credits
  2. Model Not Found
    • Verify model name spelling
    • Check if model is available in your region
  3. Rate Limiting
    • Implement exponential backoff
    • Consider upgrading your plan
  4. Token Limit Exceeded
    • Reduce input length
    • Use a model with higher token limits

Debug Mode

Enable detailed logging:

OpenAI models are constantly being updated. Check the OpenAI API documentation for the latest model availability and pricing.