> ## Documentation Index
> Fetch the complete documentation index at: https://mintlify.com/BerriAI/litellm/llms.txt
> Use this file to discover all available pages before exploring further.

# OpenAI

> Use OpenAI's GPT models through LiteLLM

## Overview

LiteLLM provides full support for OpenAI's models including GPT-4o, O1, O3-mini, and more. You can use all OpenAI features including streaming, function calling, vision, audio, and batch processing.

## Quick Start

<Steps>
  <Step title="Install LiteLLM">
    ```bash theme={null}
    pip install litellm
    ```
  </Step>

  <Step title="Set API Key">
    ```bash theme={null}
    export OPENAI_API_KEY="sk-..."
    ```
  </Step>

  <Step title="Make Your First Call">
    ```python theme={null}
    from litellm import completion

    response = completion(
        model="openai/gpt-4o",
        messages=[{"role": "user", "content": "Hello, how are you?"}]
    )
    print(response.choices[0].message.content)
    ```
  </Step>
</Steps>

## Supported Models

<Tabs>
  <Tab title="GPT-4o">
    Latest and most capable GPT-4 models with optimized performance.

    ```python theme={null}
    # GPT-4o - Best overall model
    response = completion(
        model="openai/gpt-4o",
        messages=[{"role": "user", "content": "Explain quantum computing"}]
    )

    # GPT-4o-mini - Fast and cost-effective
    response = completion(
        model="openai/gpt-4o-mini",
        messages=[{"role": "user", "content": "Summarize this text"}]
    )

    # GPT-4o with vision
    response = completion(
        model="openai/gpt-4o",
        messages=[{
            "role": "user",
            "content": [
                {"type": "text", "text": "What's in this image?"},
                {"type": "image_url", "image_url": {"url": "https://..."}}
            ]
        }]
    )
    ```
  </Tab>

  <Tab title="O-Series (Reasoning)">
    Advanced reasoning models for complex problem-solving.

    ```python theme={null}
    # O1 - Advanced reasoning
    response = completion(
        model="openai/o1",
        messages=[{"role": "user", "content": "Solve this complex math problem..."}]
    )

    # O1-mini - Efficient reasoning
    response = completion(
        model="openai/o1-mini",
        messages=[{"role": "user", "content": "Analyze this code..."}]
    )

    # O3-mini - Latest reasoning model
    response = completion(
        model="openai/o3-mini",
        messages=[{"role": "user", "content": "Debug this algorithm..."}]
    )

    # Control reasoning effort
    response = completion(
        model="openai/o1",
        messages=[{"role": "user", "content": "Complex problem..."}],
        reasoning_effort="high"  # low, medium, high
    )
    ```
  </Tab>

  <Tab title="GPT-4 Turbo">
    Previous generation GPT-4 models.

    ```python theme={null}
    # GPT-4 Turbo
    response = completion(
        model="openai/gpt-4-turbo",
        messages=[{"role": "user", "content": "Write an essay"}]
    )

    # GPT-4 Turbo with vision
    response = completion(
        model="openai/gpt-4-turbo-2024-04-09",
        messages=[{"role": "user", "content": "Analyze this chart"}]
    )
    ```
  </Tab>

  <Tab title="GPT-3.5">
    Fast and cost-effective for simpler tasks.

    ```python theme={null}
    response = completion(
        model="openai/gpt-3.5-turbo",
        messages=[{"role": "user", "content": "Quick question..."}]
    )
    ```
  </Tab>
</Tabs>

## Authentication

<Tabs>
  <Tab title="Environment Variable">
    Set your OpenAI API key as an environment variable:

    ```bash theme={null}
    export OPENAI_API_KEY="sk-..."
    ```

    ```python theme={null}
    from litellm import completion

    response = completion(
        model="openai/gpt-4o",
        messages=[{"role": "user", "content": "Hello!"}]
    )
    ```
  </Tab>

  <Tab title="Direct Parameter">
    Pass the API key directly:

    ```python theme={null}
    from litellm import completion

    response = completion(
        model="openai/gpt-4o",
        messages=[{"role": "user", "content": "Hello!"}],
        api_key="sk-..."
    )
    ```
  </Tab>

  <Tab title="Custom Base URL">
    Use a custom OpenAI-compatible endpoint:

    ```python theme={null}
    from litellm import completion

    response = completion(
        model="openai/gpt-4o",
        messages=[{"role": "user", "content": "Hello!"}],
        api_base="https://custom-openai-endpoint.com/v1",
        api_key="sk-..."
    )
    ```
  </Tab>

  <Tab title="Organization ID">
    Specify an organization for billing:

    ```python theme={null}
    from litellm import completion
    import os

    os.environ["OPENAI_ORGANIZATION"] = "org-..."

    response = completion(
        model="openai/gpt-4o",
        messages=[{"role": "user", "content": "Hello!"}]
    )
    ```
  </Tab>
</Tabs>

## Streaming

Get real-time responses as they're generated:

```python theme={null}
from litellm import completion

response = completion(
    model="openai/gpt-4o",
    messages=[{"role": "user", "content": "Write a long story"}],
    stream=True
)

for chunk in response:
    if chunk.choices[0].delta.content:
        print(chunk.choices[0].delta.content, end="", flush=True)
```

### Async Streaming

```python theme={null}
from litellm import acompletion
import asyncio

async def stream_response():
    response = await acompletion(
        model="openai/gpt-4o",
        messages=[{"role": "user", "content": "Write a story"}],
        stream=True
    )
    
    async for chunk in response:
        if chunk.choices[0].delta.content:
            print(chunk.choices[0].delta.content, end="", flush=True)

asyncio.run(stream_response())
```

## Function Calling

OpenAI models support sophisticated function/tool calling:

<CodeGroup>
  ```python Basic Function Call theme={null}
  from litellm import completion

  tools = [{
      "type": "function",
      "function": {
          "name": "get_current_weather",
          "description": "Get the current weather in a location",
          "parameters": {
              "type": "object",
              "properties": {
                  "location": {
                      "type": "string",
                      "description": "City and state, e.g. San Francisco, CA"
                  },
                  "unit": {
                      "type": "string",
                      "enum": ["celsius", "fahrenheit"]
                  }
              },
              "required": ["location"]
          }
      }
  }]

  response = completion(
      model="openai/gpt-4o",
      messages=[{"role": "user", "content": "What's the weather in Boston?"}],
      tools=tools
  )

  if response.choices[0].message.tool_calls:
      tool_call = response.choices[0].message.tool_calls[0]
      print(f"Function: {tool_call.function.name}")
      print(f"Arguments: {tool_call.function.arguments}")
  ```

  ```python Parallel Function Calls theme={null}
  tools = [
      {"type": "function", "function": {"name": "get_weather", ...}},
      {"type": "function", "function": {"name": "get_time", ...}}
  ]

  response = completion(
      model="openai/gpt-4o",
      messages=[{
          "role": "user",
          "content": "What's the weather and time in NYC?"
      }],
      tools=tools,
      parallel_tool_calls=True  # Allow multiple tool calls at once
  )

  # Model can call both functions simultaneously
  for tool_call in response.choices[0].message.tool_calls:
      print(f"Function: {tool_call.function.name}")
  ```

  ```python Force Tool Usage theme={null}
  tools = [{"type": "function", "function": {...}}]

  response = completion(
      model="openai/gpt-4o",
      messages=[{"role": "user", "content": "Search for information"}],
      tools=tools,
      tool_choice="required"  # Force the model to call a tool
  )

  # Or specify a particular tool
  response = completion(
      model="openai/gpt-4o",
      messages=[{"role": "user", "content": "What's the weather?"}],
      tools=tools,
      tool_choice={"type": "function", "function": {"name": "get_weather"}}
  )
  ```
</CodeGroup>

## Vision (Multimodal)

GPT-4o and GPT-4 Turbo support image inputs:

<Tabs>
  <Tab title="Image URL">
    ```python theme={null}
    response = completion(
        model="openai/gpt-4o",
        messages=[{
            "role": "user",
            "content": [
                {"type": "text", "text": "What's in this image?"},
                {
                    "type": "image_url",
                    "image_url": {
                        "url": "https://example.com/image.jpg"
                    }
                }
            ]
        }]
    )
    ```
  </Tab>

  <Tab title="Base64 Image">
    ```python theme={null}
    import base64

    with open("image.jpg", "rb") as image_file:
        base64_image = base64.b64encode(image_file.read()).decode('utf-8')

    response = completion(
        model="openai/gpt-4o",
        messages=[{
            "role": "user",
            "content": [
                {"type": "text", "text": "Describe this image"},
                {
                    "type": "image_url",
                    "image_url": {
                        "url": f"data:image/jpeg;base64,{base64_image}"
                    }
                }
            ]
        }]
    )
    ```
  </Tab>

  <Tab title="Multiple Images">
    ```python theme={null}
    response = completion(
        model="openai/gpt-4o",
        messages=[{
            "role": "user",
            "content": [
                {"type": "text", "text": "Compare these images"},
                {"type": "image_url", "image_url": {"url": "https://..."}},
                {"type": "image_url", "image_url": {"url": "https://..."}}
            ]
        }]
    )
    ```
  </Tab>

  <Tab title="Image Detail Level">
    ```python theme={null}
    response = completion(
        model="openai/gpt-4o",
        messages=[{
            "role": "user",
            "content": [
                {"type": "text", "text": "Analyze this in detail"},
                {
                    "type": "image_url",
                    "image_url": {
                        "url": "https://...",
                        "detail": "high"  # low, high, or auto
                    }
                }
            ]
        }]
    )
    ```
  </Tab>
</Tabs>

## JSON Mode

Force models to return valid JSON:

<CodeGroup>
  ```python JSON Mode theme={null}
  response = completion(
      model="openai/gpt-4o",
      messages=[{
          "role": "user",
          "content": "Extract info: John is 30 years old and lives in NYC"
      }],
      response_format={"type": "json_object"}
  )

  import json
  data = json.loads(response.choices[0].message.content)
  ```

  ```python Structured Output theme={null}
  # Define a schema for structured output
  response = completion(
      model="openai/gpt-4o",
      messages=[{
          "role": "user",
          "content": "Generate a person profile"
      }],
      response_format={
          "type": "json_schema",
          "json_schema": {
              "name": "person",
              "strict": True,
              "schema": {
                  "type": "object",
                  "properties": {
                      "name": {"type": "string"},
                      "age": {"type": "number"},
                      "city": {"type": "string"}
                  },
                  "required": ["name", "age", "city"],
                  "additionalProperties": False
              }
          }
      }
  )
  ```
</CodeGroup>

## Advanced Features

### Seed for Reproducibility

```python theme={null}
response = completion(
    model="openai/gpt-4o",
    messages=[{"role": "user", "content": "Tell me a joke"}],
    seed=123,  # Same seed + inputs = similar outputs
    temperature=0.7
)
```

### Logprobs

```python theme={null}
response = completion(
    model="openai/gpt-4o",
    messages=[{"role": "user", "content": "Say 'hello'"}],
    logprobs=True,
    top_logprobs=3  # Return top 3 token probabilities
)

for token in response.choices[0].logprobs.content:
    print(f"Token: {token.token}, Logprob: {token.logprob}")
```

### Max Tokens and Stop Sequences

```python theme={null}
response = completion(
    model="openai/gpt-4o",
    messages=[{"role": "user", "content": "Write a story"}],
    max_tokens=500,  # Limit output length
    stop=["\n\n", "The End"]  # Stop at these sequences
)
```

### Temperature and Top P

```python theme={null}
# More creative (temperature)
response = completion(
    model="openai/gpt-4o",
    messages=[{"role": "user", "content": "Write a poem"}],
    temperature=1.5  # 0 = deterministic, 2 = very random
)

# Nucleus sampling (top_p)
response = completion(
    model="openai/gpt-4o",
    messages=[{"role": "user", "content": "Generate text"}],
    top_p=0.9  # Consider tokens in top 90% probability mass
)
```

## Embeddings

Generate text embeddings for semantic search and clustering:

```python theme={null}
from litellm import embedding

# Single text
response = embedding(
    model="openai/text-embedding-3-large",
    input="Hello world"
)
print(response.data[0].embedding)  # List of floats

# Multiple texts
response = embedding(
    model="openai/text-embedding-3-small",
    input=["Text 1", "Text 2", "Text 3"]
)

for item in response.data:
    print(f"Index {item.index}: {len(item.embedding)} dimensions")

# Specify dimensions (3-large and 3-small support this)
response = embedding(
    model="openai/text-embedding-3-large",
    input="Hello world",
    dimensions=256  # Reduce from default 3072
)
```

### Available Embedding Models

| Model                    | Dimensions     | Use Case         |
| ------------------------ | -------------- | ---------------- |
| `text-embedding-3-large` | 3072 (default) | Best performance |
| `text-embedding-3-small` | 1536 (default) | Good balance     |
| `text-embedding-ada-002` | 1536           | Legacy model     |

## Batch Processing

Process large volumes of requests asynchronously:

```python theme={null}
from litellm import create_batch, retrieve_batch

# Create a batch job
batch = create_batch(
    custom_llm_provider="openai",
    input_file_id="file-abc123",  # Upload file first
    endpoint="/v1/chat/completions",
    completion_window="24h"
)

print(f"Batch ID: {batch.id}")
print(f"Status: {batch.status}")

# Check batch status
batch_status = retrieve_batch(
    custom_llm_provider="openai",
    batch_id=batch.id
)

print(f"Completed: {batch_status.request_counts.completed}")
print(f"Failed: {batch_status.request_counts.failed}")
```

## Error Handling

```python theme={null}
from litellm import completion
from litellm.exceptions import (
    AuthenticationError,
    RateLimitError,
    ContextWindowExceededError,
    APIError
)

try:
    response = completion(
        model="openai/gpt-4o",
        messages=[{"role": "user", "content": "Hello!"}]
    )
except AuthenticationError:
    print("Invalid API key")
except RateLimitError:
    print("Rate limit exceeded - retry later")
except ContextWindowExceededError:
    print("Message too long - reduce input size")
except APIError as e:
    print(f"API error: {e}")
```

## Cost Tracking

```python theme={null}
from litellm import completion, completion_cost

response = completion(
    model="openai/gpt-4o",
    messages=[{"role": "user", "content": "Hello!"}]
)

# Calculate cost
cost = completion_cost(completion_response=response)
print(f"Cost: ${cost:.6f}")

# Response includes token usage
print(f"Prompt tokens: {response.usage.prompt_tokens}")
print(f"Completion tokens: {response.usage.completion_tokens}")
print(f"Total tokens: {response.usage.total_tokens}")
```

## Best Practices

<CardGroup cols={2}>
  <Card title="Use GPT-4o-mini First" icon="bolt">
    Start with `gpt-4o-mini` for testing - it's fast and cost-effective. Upgrade to `gpt-4o` when you need maximum quality.
  </Card>

  <Card title="Set Max Tokens" icon="gauge">
    Always set `max_tokens` to prevent unexpectedly long (and expensive) responses.
  </Card>

  <Card title="Use Streaming" icon="wave-pulse">
    Enable streaming for better user experience in interactive applications.
  </Card>

  <Card title="Handle Rate Limits" icon="shield">
    Implement exponential backoff when handling `RateLimitError` exceptions.
  </Card>
</CardGroup>

## Related Documentation

<CardGroup cols={2}>
  <Card title="Streaming" icon="wave-pulse" href="/providers/streaming">
    Learn more about streaming responses
  </Card>

  <Card title="Function Calling" icon="function" href="/providers/function-calling">
    Deep dive into function calling
  </Card>

  <Card title="Vision" icon="eye" href="/providers/vision">
    Working with images and vision models
  </Card>

  <Card title="Embeddings" icon="vector-square" href="/providers/embeddings">
    Guide to embeddings and semantic search
  </Card>
</CardGroup>
