{"id":2574,"library":"llama-index-agent-openai","title":"LlamaIndex OpenAI Agent","description":"The `llama-index-agent-openai` library provides an integration for creating agents within LlamaIndex that leverage OpenAI's language models. It allows defining tools and orchestrating them with OpenAI's function-calling capabilities, making it easy to build powerful LLM-powered applications. As part of the LlamaIndex ecosystem, it stays current with LlamaIndex's modular architecture (v0.10+). The current version is `0.4.12`, with updates typically aligning with LlamaIndex core releases.","status":"active","version":"0.4.12","language":"en","source_language":"en","source_url":"https://github.com/run-llama/llama_index/tree/main/llama-index-integrations/agent/llama-index-agent-openai","tags":["llama-index","openai","agent","llm","ai","chatbot","tool-use"],"install":[{"cmd":"pip install llama-index-agent-openai","lang":"bash","label":"Install LlamaIndex OpenAI Agent"}],"dependencies":[{"reason":"Core LlamaIndex framework functionality, including agents and tools.","package":"llama-index>=0.10.12","optional":false},{"reason":"Required for interacting with OpenAI API, including chat completions and function calling.","package":"openai>=1.1.0","optional":false}],"imports":[{"note":"The modular LlamaIndex (v0.10+) moved integrations to dedicated packages, standardizing the import path to `llama_index.agent.openai`.","wrong":"from llama_index.agent.openai_agent import OpenAIAgent","symbol":"OpenAIAgent","correct":"from llama_index.agent.openai import OpenAIAgent"},{"note":"OpenAI LLM is now found within the `llama-index-llms-openai` package, not directly from `llama_index.llms`.","wrong":"from llama_index.llms import OpenAI","symbol":"OpenAI LLM","correct":"from llama_index.llms.openai import OpenAI"}],"quickstart":{"code":"import os\nfrom llama_index.agent.openai import OpenAIAgent\nfrom llama_index.llms.openai import OpenAI\nfrom llama_index.core.tools import FunctionTool\n\ndef multiply(a: int, b: int) -> int:\n    \"\"\"Multiply two integers and return the result integer\"\"\"\n    return a * b\n\n# Define a tool from a function\nmultiply_tool = FunctionTool.from_defaults(fn=multiply)\n\n# Initialize LLM with API key (ensure OPENAI_API_KEY is set in environment)\nllm = OpenAI(\n    model=\"gpt-3.5-turbo\",\n    api_key=os.environ.get('OPENAI_API_KEY', '') # Use os.environ.get for security\n)\n\n# Initialize OpenAI agent with tools\nagent = OpenAIAgent.from_tools(\n    tools=[multiply_tool],\n    llm=llm,\n    verbose=True,\n)\n\n# Chat with the agent\nresponse = agent.chat(\"What is 2 * 2?\")\nprint(f\"Agent Response: {response}\")\n\nresponse_complex = agent.chat(\"What is 10 times 5 plus 3?\")\nprint(f\"Agent Response (complex): {response_complex}\")","lang":"python","description":"This quickstart demonstrates how to initialize an `OpenAIAgent` with a custom `FunctionTool` and use it to perform calculations through conversational prompts. Ensure your `OPENAI_API_KEY` environment variable is set for successful authentication with OpenAI."},"warnings":[{"fix":"Upgrade your core `llama-index` installation to version `0.10.12` or newer using `pip install --upgrade llama-index llama-index-agent-openai`.","message":"LlamaIndex Modularization (v0.10+): `llama-index-agent-openai` is designed exclusively for the modular LlamaIndex (v0.10 and later). It explicitly requires `llama-index>=0.10.12`. Attempts to use it with older, monolithic LlamaIndex versions (e.g., v0.9.x) will result in import errors or runtime failures due to incompatible API structures.","severity":"breaking","affected_versions":"<0.10.12 of llama-index"},{"fix":"Ensure your `openai` package is `1.1.0` or newer: `pip install --upgrade openai`.","message":"`openai` Python package version: This integration requires `openai>=1.1.0`. Using older `openai` versions (e.g., `0.x.x`) will cause `APIError` or `TypeError` due to significant changes in `openai`'s API, especially regarding client initialization and method calls (e.g., `openai.Completion` vs `client.chat.completions.create`).","severity":"gotcha","affected_versions":"<1.1.0 of openai"},{"fix":"Always use `OpenAIAgent.from_tools(...)` for initializing agents with a list of tools.","message":"`OpenAIAgent` initialization with tools: The recommended and most robust way to initialize `OpenAIAgent` is using `OpenAIAgent.from_tools()`. Directly passing a `tools` argument to the constructor, or using older initialization patterns, might behave differently or be deprecated in future versions, potentially leading to incorrect tool invocation or agent behavior.","severity":"gotcha","affected_versions":"All (best practice)"},{"fix":"Set the `OPENAI_API_KEY` environment variable (e.g., `export OPENAI_API_KEY='sk-...'`) or pass it directly: `llm = OpenAI(api_key='sk-...')`.","message":"OpenAI API Key Configuration: The OpenAI API key (traditionally `OPENAI_API_KEY`) must be correctly configured. If it's not set as an environment variable, it must be explicitly passed to the `OpenAI` LLM instance during initialization. Forgetting this will result in `AuthenticationError` from the OpenAI API.","severity":"gotcha","affected_versions":"All (configuration)"}],"env_vars":null,"last_verified":"2026-04-10T00:00:00.000Z","next_check":"2026-07-09T00:00:00.000Z"}