Environment:Deepset ai Haystack OpenAI API Environment
| Knowledge Sources | |
|---|---|
| Domains | Infrastructure, LLMs |
| Last Updated | 2026-02-11 20:00 GMT |
Overview
OpenAI API environment requiring an API key and the `openai` >= 1.99.2 Python package for LLM generation and chat components.
Description
This environment provides the configuration needed to use OpenAI-powered components in Haystack, including `OpenAIGenerator` for text completion and `OpenAIChatGenerator` for chat-based generation with tool calling support. It also covers Azure OpenAI endpoints which use the same underlying SDK. The `openai` package is a core dependency of Haystack (not optional), but an API key is required at runtime to actually call the OpenAI API.
Usage
Use this environment when running any pipeline that includes OpenAIGenerator, OpenAIChatGenerator, OpenAIDocumentEmbedder, OpenAITextEmbedder, or LLM-based evaluators (FaithfulnessEvaluator, ContextRelevanceEvaluator) that default to OpenAI models.
System Requirements
| Category | Requirement | Notes |
|---|---|---|
| OS | OS Independent | Any platform with internet access |
| Network | Outbound HTTPS | Must reach `api.openai.com` (or Azure endpoint) |
| Python | >= 3.10 | Inherited from base Haystack requirement |
Dependencies
Python Packages
- `openai` >= 1.99.2 (included in core Haystack dependencies)
Credentials
The following environment variables must be set:
- `OPENAI_API_KEY`: OpenAI API key for authentication. Used by default via `Secret.from_env_var("OPENAI_API_KEY")` in OpenAI components.
For Azure OpenAI:
- `AZURE_OPENAI_API_KEY`: Azure OpenAI API key
- `AZURE_OPENAI_ENDPOINT`: Azure OpenAI endpoint URL (e.g., `https://your-resource.openai.azure.com/`)
Quick Install
# OpenAI is already included in haystack-ai core dependencies
pip install haystack-ai
# Set your API key
export OPENAI_API_KEY="your-api-key-here"
Code Evidence
Secret resolution pattern from `haystack/components/generators/openai.py:32-50`:
class OpenAIGenerator:
def __init__(
self,
api_key: Secret = Secret.from_env_var("OPENAI_API_KEY"),
model: str = "gpt-4o-mini",
...
):
Secret.from_env_var mechanism from `haystack/utils/auth.py:57-72`:
@staticmethod
def from_env_var(env_vars: str | list[str], *, strict: bool = True) -> "Secret":
"""
Create an environment variable-based secret. Accepts one or more environment variables.
Upon resolution, it returns a string token from the first environment variable that is set.
"""
if isinstance(env_vars, str):
env_vars = [env_vars]
return EnvVarSecret(_env_vars=tuple(env_vars), _strict=strict)
OpenAI dependency from `pyproject.toml:47`:
"openai>=1.99.2",
Common Errors
| Error Message | Cause | Solution |
|---|---|---|
| `openai.AuthenticationError` | Missing or invalid API key | Set `OPENAI_API_KEY` environment variable with a valid key |
| `Secret 'OPENAI_API_KEY' not found` | Environment variable not set and strict=True | `export OPENAI_API_KEY="sk-..."` before running the pipeline |
| `openai.RateLimitError` | API rate limit exceeded | Reduce request frequency or upgrade OpenAI plan |
Compatibility Notes
- OpenAI SDK version: Requires >= 1.99.2; older versions may lack streaming or tool calling features
- Azure OpenAI: Uses same SDK but different authentication; set Azure-specific env vars instead
- Custom endpoints: The `api_base_url` parameter allows pointing to compatible API endpoints (e.g., local LLM servers)