Skip to content

OpenAI

OpenAI is an AI research and deployment company that provides a suite of powerful language models. The Strands Agents SDK implements an OpenAI provider, allowing you to run agents against any OpenAI or OpenAI-compatible model.

OpenAI is configured as an optional dependency in Strands Agents. To install, run:

Terminal window
pip install 'strands-agents[openai]' strands-agents-tools

After installing dependencies, you can import and initialize the Strands Agents’ OpenAI provider as follows:

from strands import Agent
from strands.models.openai import OpenAIModel
from strands_tools import calculator
model = OpenAIModel(
client_args={
"api_key": "<KEY>",
},
# **model_config
model_id="gpt-4o",
params={
"max_tokens": 1000,
"temperature": 0.7,
}
)
agent = Agent(model=model, tools=[calculator])
response = agent("What is 2+2")
print(response)

To connect to a custom OpenAI-compatible server:

model = OpenAIModel(
client_args={
"api_key": "<KEY>",
"base_url": "<URL>",
},
...
)

The client_args configure the underlying OpenAI client. For a complete list of available arguments, please refer to the OpenAI source.

The model configuration sets parameters for inference:

ParameterDescriptionExampleOptions
model_idID of a model to usegpt-4oreference
paramsModel specific parameters{"max_tokens": 1000, "temperature": 0.7}reference

Module Not Found

If you encounter the error ModuleNotFoundError: No module named 'openai', this means you haven’t installed the openai dependency in your environment. To fix, run pip install 'strands-agents[openai]'.

OpenAI models support structured output through their native tool calling capabilities. When you use Agent.structured_output(), the Strands SDK automatically converts your schema to OpenAI’s function calling format.

from pydantic import BaseModel, Field
from strands import Agent
from strands.models.openai import OpenAIModel
class PersonInfo(BaseModel):
"""Extract person information from text."""
name: str = Field(description="Full name of the person")
age: int = Field(description="Age in years")
occupation: str = Field(description="Job or profession")
model = OpenAIModel(
client_args={"api_key": "<KEY>"},
model_id="gpt-4o",
)
agent = Agent(model=model)
result = agent.structured_output(
PersonInfo,
"John Smith is a 30-year-old software engineer working at a tech startup."
)
print(f"Name: {result.name}") # "John Smith"
print(f"Age: {result.age}") # 30
print(f"Job: {result.occupation}") # "software engineer"

Users can pass their own custom OpenAI client to the OpenAIModel for Strands Agents to use directly. Users are responsible for handling the lifecycle (e.g., closing) of the client.

from strands import Agent
from strands.models.openai import OpenAIModel
from openai import AsyncOpenAI
client = AsyncOpenAI(
api_key= "<KEY>",
)
agent = Agent(
model = OpenAIModel(
model_id="gpt-4o-mini-2024-07-18",
client=client
)
)
async def chat(prompt: str):
result = await agent.invoke_async(prompt)
print(result)
async def main():
await chat("What is 2+2")
await chat("What is 2*2")
# close the client
client.close()
if __name__ == "__main__":
asyncio.run(main())