Environment:Run llama Llama index OpenAI API Configuration
| Knowledge Sources | |
|---|---|
| Domains | Infrastructure, LLM_Finetuning, Evaluation |
| Last Updated | 2026-02-11 19:00 GMT |
Overview
OpenAI API key configuration required for LLM finetuning, evaluation, and default LLM/embedding model usage in LlamaIndex.
Description
This environment defines the API credentials needed to interact with OpenAI services. The `OPENAI_API_KEY` is used by the `OpenAIFinetuneEngine` for launching and monitoring finetuning jobs, by the evaluation pipeline for LLM-as-judge evaluators, and as the default LLM provider when using the `llama-index` meta-package (which includes `llama-index-llms-openai`).
Usage
Use this environment when running any workflow that calls OpenAI APIs: OpenAI LLM Finetuning, Evaluation Pipeline (with OpenAI evaluator LLM), or RAG Query Pipeline with default OpenAI LLM/embedding configuration.
System Requirements
| Category | Requirement | Notes |
|---|---|---|
| Network | Internet access to api.openai.com | Required for all API calls |
| Account | OpenAI API account with billing | Finetuning requires paid tier |
Dependencies
Python Packages
- `openai` (pulled in by `llama-index-llms-openai`)
- `llama-index-llms-openai` >= 0.6.0, < 0.7
Credentials
The following environment variables must be configured:
- `OPENAI_API_KEY`: OpenAI API key for authentication. Used by `OpenAIFinetuneEngine` and default LLM/embedding resolution.
For Azure OpenAI users, additional variables are needed:
- `AZURE_OPENAI_API_KEY`: Azure-specific API key
- `AZURE_OPENAI_ENDPOINT`: Azure endpoint URL
- `OPENAI_API_VERSION`: API version (defaults to `2024-02-01`)
Quick Install
# Install OpenAI LLM integration
pip install llama-index-llms-openai>=0.6.0
# Set the API key
export OPENAI_API_KEY="sk-..."
Code Evidence
OpenAI client initialization from `openai/base.py:38`:
self._client = SyncOpenAI(api_key=os.getenv("OPENAI_API_KEY", None))
Azure OpenAI client initialization from `azure_openai/base.py:37-39`:
azure_endpoint=os.getenv("AZURE_OPENAI_ENDPOINT"),
api_key=os.getenv("AZURE_OPENAI_API_KEY", None),
api_version=os.getenv("OPENAI_API_VERSION", "2024-02-01"),
Generic environment resolution pattern from `generic_utils.py:311-329`:
def get_from_param_or_env(
key: str,
param: Optional[str] = None,
env_key: Optional[str] = None,
default: Optional[str] = None,
) -> str:
if param is not None:
return param
elif env_key and env_key in os.environ and os.environ[env_key]:
return os.environ[env_key]
elif default is not None:
return default
else:
raise ValueError(
f"Did not find {key}, please add an environment variable"
f" `{env_key}` which contains it, or pass"
f" `{key}` as a named parameter."
)
Common Errors
| Error Message | Cause | Solution |
|---|---|---|
| `AuthenticationError: Incorrect API key provided` | Invalid or expired OPENAI_API_KEY | Verify API key at platform.openai.com and re-export |
| `ValueError: Did not find api_key, please add an environment variable` | OPENAI_API_KEY not set | `export OPENAI_API_KEY="sk-..."` |
| `RateLimitError` | API rate limits exceeded | Reduce batch eval workers or add retry logic |
| `InsufficientQuotaError` | Billing limits reached | Check OpenAI billing dashboard |
Compatibility Notes
- Azure OpenAI: Fully supported via `AzureOpenAIFinetuneEngine`. Requires separate credential variables (`AZURE_OPENAI_*`).
- API Key Precedence: Constructor parameter > environment variable > raises ValueError. This pattern is consistent across all LlamaIndex LLM integrations.
- Finetuning Access: OpenAI finetuning requires a paid account tier. Free-tier accounts cannot create finetuning jobs.
Related Pages
- Implementation:Run_llama_Llama_index_OpenAIFinetuneEngine_Finetune
- Implementation:Run_llama_Llama_index_OpenAIFinetuneEngine_Get_Current_Job
- Implementation:Run_llama_Llama_index_OpenAIFinetuneEngine_Get_Finetuned_Model
- Implementation:Run_llama_Llama_index_FinetuningHandler_Event_Capture
- Implementation:Run_llama_Llama_index_Validate_Json
- Implementation:Run_llama_Llama_index_Settings_LLM_Assignment
- Implementation:Run_llama_Llama_index_Evaluator_Init
- Implementation:Run_llama_Llama_index_DatasetGenerator_From_Documents