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.
Installation¶
OpenAI is configured as an optional dependency in Strands Agents. To install, run:
pip install 'strands-agents[openai]' strands-agents-tools
npm install @strands-agents/sdk
Usage¶
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)
import { Agent } from '@strands-agents/sdk'
import { OpenAIModel } from '@strands-agents/sdk/models/openai'
const model = new OpenAIModel({
apiKey: process.env.OPENAI_API_KEY || '<KEY>',
modelId: 'gpt-4o',
maxTokens: 1000,
temperature: 0.7,
})
const agent = new Agent({ model })
const response = await agent.invoke('What is 2+2')
console.log(response)
To connect to a custom OpenAI-compatible server:
model = OpenAIModel(
client_args={
"api_key": "<KEY>",
"base_url": "<URL>",
},
...
)
const model = new OpenAIModel({
apiKey: '<KEY>',
clientConfig: {
baseURL: '<URL>',
},
modelId: 'gpt-4o',
})
const agent = new Agent({ model })
const response = await agent.invoke('Hello!')
Configuration¶
Client Configuration¶
The client_args configure the underlying OpenAI client. For a complete list of available arguments, please refer to the OpenAI source.
The clientConfig configures the underlying OpenAI client. For a complete list of available options, please refer to the OpenAI TypeScript documentation.
Model Configuration¶
The model configuration sets parameters for inference:
| Parameter | Description | Example | Options |
|---|---|---|---|
model_id |
ID of a model to use | gpt-4o |
reference |
params |
Model specific parameters | {"max_tokens": 1000, "temperature": 0.7} |
reference |
| Parameter | Description | Example | Options |
|---|---|---|---|
modelId |
ID of a model to use | gpt-4o |
reference |
maxTokens |
Maximum tokens to generate | 1000 |
reference |
temperature |
Controls randomness (0-2) | 0.7 |
reference |
topP |
Nucleus sampling (0-1) | 0.9 |
reference |
frequencyPenalty |
Reduces repetition (-2.0 to 2.0) | 0.5 |
reference |
presencePenalty |
Encourages new topics (-2.0 to 2.0) | 0.5 |
reference |
params |
Additional parameters not listed above | { stop: ["END"] } |
reference |
Troubleshooting¶
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]'.
Authentication Errors
If you encounter authentication errors, ensure your OpenAI API key is properly configured. Set the OPENAI_API_KEY environment variable or pass it via the apiKey parameter in the model configuration.
Advanced Features¶
Structured Output¶
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"
// Structured output is not yet supported in the TypeScript SDK