generated from amazon-archives/__template_Apache-2.0
-
Notifications
You must be signed in to change notification settings - Fork 195
docs: add page for OpenAI Responses API model provider #590
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
notgitika
wants to merge
1
commit into
strands-agents:main
Choose a base branch
from
notgitika:notgitika/openai-responses-docs
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+134
−5
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
122 changes: 122 additions & 0 deletions
122
docs/user-guide/concepts/model-providers/openai-responses.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,122 @@ | ||
| # OpenAI Responses API | ||
|
|
||
| !!! info "Language Support" | ||
| This provider is only supported in Python. | ||
|
|
||
| The [OpenAI Responses API](https://platform.openai.com/docs/api-reference/responses/create) is OpenAI's latest API for interacting with their models. The Strands Agents SDK provides a dedicated `OpenAIResponsesModel` provider that uses this API, supporting streaming, tool calling, and structured output. | ||
|
|
||
| !!! note "Looking for the Chat Completions API?" | ||
| If you want to use OpenAI's Chat Completions API instead, see the [OpenAI (Chat Completions)](openai.md) provider. | ||
|
|
||
| ## Installation | ||
|
|
||
| OpenAI is configured as an optional dependency in Strands Agents. The Responses API provider requires the OpenAI Python SDK **v2.0.0 or later**. To install, run: | ||
|
|
||
| ```bash | ||
| pip install 'strands-agents[openai]' strands-agents-tools | ||
notgitika marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ``` | ||
|
|
||
| ## Usage | ||
|
|
||
| After installing dependencies, you can import and initialize the OpenAI Responses API provider as follows: | ||
|
|
||
| ```python | ||
| from strands import Agent | ||
| from strands.models.openai_responses import OpenAIResponsesModel | ||
| from strands_tools import calculator | ||
|
|
||
| model = OpenAIResponsesModel( | ||
| client_args={ | ||
| "api_key": "<KEY>", | ||
| }, | ||
| model_id="gpt-4o", | ||
| params={ | ||
| "max_output_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: | ||
|
|
||
| ```python | ||
| model = OpenAIResponsesModel( | ||
| client_args={ | ||
| "api_key": "<KEY>", | ||
| "base_url": "<URL>", | ||
| }, | ||
| ... | ||
| ) | ||
| ``` | ||
|
|
||
| ## Configuration | ||
|
|
||
| ### Client Configuration | ||
|
|
||
| The `client_args` configure the underlying `AsyncOpenAI` client. For a complete list of available arguments, please refer to the OpenAI [source](https://github.com/openai/openai-python). | ||
|
|
||
| ### Model Configuration | ||
|
|
||
| The model configuration sets parameters for inference: | ||
|
|
||
| | Parameter | Description | Example | Options | | ||
| |-----------|-------------|---------|---------| | ||
| | `model_id` | ID of a model to use | `gpt-4o` | [reference](https://platform.openai.com/docs/models) | | ||
| | `params` | Model specific parameters | `{"max_output_tokens": 1000, "temperature": 0.7}` | [reference](https://platform.openai.com/docs/api-reference/responses/create) | | ||
|
|
||
| ## 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]'`. | ||
|
|
||
| **OpenAI SDK Version** | ||
|
|
||
| The Responses API provider requires the OpenAI Python SDK v2.0.0 or later. If you encounter an `ImportError` about the SDK version, upgrade with `pip install --upgrade openai`. | ||
|
|
||
| ## Advanced Features | ||
|
|
||
| ### Structured Output | ||
|
|
||
| The OpenAI Responses API provider supports structured output through the `responses.parse(...)` endpoint. When you use `Agent.structured_output()`, the Strands SDK uses this to return typed results conforming to your Pydantic model. | ||
|
|
||
| ```python | ||
| from pydantic import BaseModel, Field | ||
| from strands import Agent | ||
| from strands.models.openai_responses import OpenAIResponsesModel | ||
|
|
||
| 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 = OpenAIResponsesModel( | ||
| 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" | ||
| ``` | ||
|
|
||
| ### Reasoning Models | ||
|
|
||
| The Responses API provider supports reasoning models (such as o1 and o3) that include chain-of-thought reasoning in their responses. Reasoning content is automatically captured and streamed as `reasoningContent` events. | ||
|
|
||
| ## References | ||
|
|
||
| - [OpenAI Responses API](https://platform.openai.com/docs/api-reference/responses/create) | ||
| - [OpenAI (Chat Completions) Provider](openai.md) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As a customer, how do I choose between the two? Which one is recommended/deprecated?
Assume someone who wants to develop agent with some openai model. which one should they choose?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For the python sdk, I think the obvious path should be the chat api, since thats what the expectation is today. Moving forward to typescript, we might want to flip that to be responses by default. Power users should be able to investigate this and switch between the two api's though