Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
153 changes: 48 additions & 105 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,35 +36,14 @@ client = OpenAI(
)

response = client.responses.create(
model="gpt-5.5",
model="gpt-6.2-sol",
instructions="You are a coding assistant that talks like a pirate.",
input="How do I check if a Python object is an instance of a class?",
)

print(response.output_text)
```

The previous standard (supported indefinitely) for generating text is the [Chat Completions API](https://platform.openai.com/docs/api-reference/chat). You can use that API to generate text from the model with the code below.

```python
from openai import OpenAI

client = OpenAI()

completion = client.chat.completions.create(
model="gpt-5.5",
messages=[
{"role": "developer", "content": "Talk like a pirate."},
{
"role": "user",
"content": "How do I check if a Python object is an instance of a class?",
},
],
)

print(completion.choices[0].message.content)
```

While you can provide an `api_key` keyword argument,
we recommend using [python-dotenv](https://pypi.org/project/python-dotenv/)
to add `OPENAI_API_KEY="My API Key"` to your `.env` file
Expand All @@ -89,9 +68,9 @@ client = OpenAI(
},
)

response = client.chat.completions.create(
model="gpt-5.5",
messages=[{"role": "user", "content": "Hello!"}],
response = client.responses.create(
model="gpt-6.2-sol",
input="Hello!",
)
```

Expand Down Expand Up @@ -233,7 +212,7 @@ prompt = "What is in this image?"
img_url = "https://api.nga.gov/iiif/a2e6da57-3cd1-4235-b20e-95dcaefed6c8/full/!800,800/0/default.jpg"

response = client.responses.create(
model="gpt-5.5",
model="gpt-6.2-sol",
input=[
{
"role": "user",
Expand All @@ -259,7 +238,7 @@ with open("path/to/image.png", "rb") as image_file:
b64_image = base64.b64encode(image_file.read()).decode("utf-8")

response = client.responses.create(
model="gpt-5.5",
model="gpt-6.2-sol",
input=[
{
"role": "user",
Expand Down Expand Up @@ -289,7 +268,7 @@ client = AsyncOpenAI(

async def main() -> None:
response = await client.responses.create(
model="gpt-5.5", input="Explain disestablishmentarianism to a smart five year old."
model="gpt-6.2-sol", input="Explain disestablishmentarianism to a smart five year old."
)
print(response.output_text)

Expand Down Expand Up @@ -324,15 +303,11 @@ async def main() -> None:
api_key=os.environ.get("OPENAI_API_KEY"), # This is the default and can be omitted
http_client=DefaultAioHttpClient(),
) as client:
chat_completion = await client.chat.completions.create(
messages=[
{
"role": "user",
"content": "Say this is a test",
}
],
model="gpt-5.5",
response = await client.responses.create(
model="gpt-6.2-sol",
input="Say this is a test",
)
print(response.output_text)


asyncio.run(main())
Expand All @@ -352,7 +327,7 @@ from openai import OpenAI
client = OpenAI()

stream = client.responses.create(
model="gpt-5.5",
model="gpt-6.2-sol",
input="Write a one-sentence bedtime story about a unicorn.",
stream=True,
)
Expand All @@ -372,7 +347,7 @@ client = AsyncOpenAI()

async def main():
stream = await client.responses.create(
model="gpt-5.5",
model="gpt-6.2-sol",
input="Write a one-sentence bedtime story about a unicorn.",
stream=True,
)
Expand Down Expand Up @@ -552,10 +527,10 @@ response = client.responses.create(
input=[
{
"role": "user",
"content": "How much ?",
"content": "Return the price of a $12 item after a 25% discount as a JSON object.",
}
],
model="gpt-5.5",
model="gpt-6.2-sol",
text={"format": {"type": "json_object"}},
)
```
Expand Down Expand Up @@ -734,7 +709,7 @@ All object responses in the SDK provide a `_request_id` property which is added

```python
response = await client.responses.create(
model="gpt-5.5",
model="gpt-6.2-sol",
input="Say 'this is a test'.",
)
print(response._request_id) # req_123
Expand All @@ -751,8 +726,9 @@ methods and modules are _private_.
import openai

try:
completion = await client.chat.completions.create(
messages=[{"role": "user", "content": "Say this is a test"}], model="gpt-5.5"
response = await client.responses.create(
model="gpt-6.2-sol",
input="Say this is a test",
)
except openai.APIStatusError as exc:
print(exc.request_id) # req_123
Expand All @@ -779,14 +755,9 @@ client = OpenAI(
)

# Or, configure per-request:
client.with_options(max_retries=5).chat.completions.create(
messages=[
{
"role": "user",
"content": "How can I get the name of the current day in JavaScript?",
}
],
model="gpt-5.5",
client.with_options(max_retries=5).responses.create(
model="gpt-6.2-sol",
input="How can I get the name of the current day in JavaScript?",
)
```

Expand Down Expand Up @@ -818,14 +789,9 @@ client = OpenAI(
)

# Override per-request:
client.with_options(timeout=5.0).chat.completions.create(
messages=[
{
"role": "user",
"content": "How can I list all files in a directory using Python?",
}
],
model="gpt-5.5",
client.with_options(timeout=5.0).responses.create(
model="gpt-6.2-sol",
input="How can I list all files in a directory using Python?",
)
```

Expand Down Expand Up @@ -869,19 +835,14 @@ The "raw" Response object can be accessed by prefixing `.with_raw_response.` to
from openai import OpenAI

client = OpenAI()
response = client.chat.completions.with_raw_response.create(
messages=[
{
"role": "user",
"content": "Say this is a test",
}
],
model="gpt-5.5",
raw_response = client.responses.with_raw_response.create(
model="gpt-6.2-sol",
input="Say this is a test",
)
print(response.headers.get("X-My-Header"))
print(raw_response.headers.get("X-My-Header"))

completion = response.parse() # get the object that `chat.completions.create()` would have returned
print(completion)
response = raw_response.parse() # get the object that `responses.create()` would have returned
print(response.output_text)
```

These methods return a [`LegacyAPIResponse`](https://github.com/openai/openai-python/tree/main/src/openai/_legacy_response.py) object. This is a legacy class as we're changing it slightly in the next major version.
Expand All @@ -902,14 +863,9 @@ To stream the response body, use `.with_streaming_response` instead, which requi
As such, `.with_streaming_response` methods return a different [`APIResponse`](https://github.com/openai/openai-python/tree/main/src/openai/_response.py) object, and the async client returns an [`AsyncAPIResponse`](https://github.com/openai/openai-python/tree/main/src/openai/_response.py) object.

```python
with client.chat.completions.with_streaming_response.create(
messages=[
{
"role": "user",
"content": "Say this is a test",
}
],
model="gpt-5.5",
with client.responses.with_streaming_response.create(
model="gpt-6.2-sol",
input="Say this is a test",
) as response:
print(response.headers.get("X-My-Header"))

Expand Down Expand Up @@ -1105,7 +1061,7 @@ from openai import AsyncOpenAI

async def main() -> None:
async with AsyncOpenAI() as client:
response = await client.responses.create(model="gpt-5.5", input="Say this is a test")
response = await client.responses.create(model="gpt-6.2-sol", input="Say this is a test")
print(response.output_text)


Expand All @@ -1114,46 +1070,33 @@ asyncio.run(main())

## Microsoft Azure OpenAI

To use this library with [Azure OpenAI](https://learn.microsoft.com/azure/ai-services/openai/overview), use the `AzureOpenAI`
class instead of the `OpenAI` class.

> [!IMPORTANT]
> The Azure API shape differs from the core API shape which means that the static types for responses / params
> won't always be correct.
To use the [Azure OpenAI Responses API](https://learn.microsoft.com/azure/ai-foundry/openai/how-to/responses),
configure the `OpenAI` client with your Azure v1 endpoint and pass your deployment name as `model`.

```py
from openai import AzureOpenAI

# gets the API Key from environment variable AZURE_OPENAI_API_KEY
client = AzureOpenAI(
# https://learn.microsoft.com/azure/ai-services/openai/reference#rest-api-versioning
api_version="2023-07-01-preview",
# https://learn.microsoft.com/azure/cognitive-services/openai/how-to/create-resource?pivots=web-portal#create-a-resource
azure_endpoint="https://example-endpoint.openai.azure.com",
import os
from openai import OpenAI

client = OpenAI(
api_key=os.environ["AZURE_OPENAI_API_KEY"],
base_url="https://example-endpoint.openai.azure.com/openai/v1/",
)

completion = client.chat.completions.create(
model="deployment-name", # e.g. gpt-35-instant
messages=[
{
"role": "user",
"content": "How do I output all files in a directory using Python?",
},
],
response = client.responses.create(
model="deployment-name",
input="How do I output all files in a directory using Python?",
)
print(completion.to_json())
print(response.output_text)
```

In addition to the options provided in the base `OpenAI` client, the following options are provided:
For versioned Azure endpoints, the `AzureOpenAI` client supports these additional options:

- `azure_endpoint` (or the `AZURE_OPENAI_ENDPOINT` environment variable)
- `azure_deployment`
- `api_version` (or the `OPENAI_API_VERSION` environment variable)
- `azure_ad_token` (or the `AZURE_OPENAI_AD_TOKEN` environment variable)
- `azure_ad_token_provider`

An example of using the client with Microsoft Entra ID (formerly known as Azure Active Directory) can be found [here](https://github.com/openai/openai-python/blob/main/examples/azure_ad.py).

## Amazon Bedrock

To use this library with [Amazon Bedrock's OpenAI-compatible API](https://docs.aws.amazon.com/bedrock/latest/userguide/models-api-compatibility.html), configure the standard `OpenAI` client with the Bedrock provider.
Expand Down
2 changes: 1 addition & 1 deletion examples/async_demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

async def main() -> None:
stream = await client.chat.completions.create(
model="gpt-5.5",
model="gpt-6.2-sol",
messages=[
{
"role": "user",
Expand Down
6 changes: 3 additions & 3 deletions examples/demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
# Non-streaming:
print("----- standard request -----")
completion = client.chat.completions.create(
model="gpt-5.5",
model="gpt-6.2-sol",
messages=[
{
"role": "user",
Expand All @@ -22,7 +22,7 @@
# Streaming:
print("----- streaming request -----")
stream = client.chat.completions.create(
model="gpt-5.5",
model="gpt-6.2-sol",
messages=[
{
"role": "user",
Expand All @@ -41,7 +41,7 @@
# Response headers:
print("----- custom response headers test -----")
response = client.chat.completions.with_raw_response.create(
model="gpt-5.5",
model="gpt-6.2-sol",
messages=[
{
"role": "user",
Expand Down
2 changes: 1 addition & 1 deletion examples/httpx2_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
client = OpenAI(http_client=DefaultHttpx2Client())

response = client.responses.create(
model="gpt-5.2",
model="gpt-6.2-sol",
input="Say this is a test",
)

Expand Down
2 changes: 1 addition & 1 deletion examples/module_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

# all API calls work in the exact same fashion as well
stream = openai.chat.completions.create(
model="gpt-5.6",
model="gpt-6.2-sol",
messages=[
{
"role": "user",
Expand Down
2 changes: 1 addition & 1 deletion examples/parsing.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class MathResponse(BaseModel):
client = OpenAI()

completion = client.chat.completions.parse(
model="gpt-4o-2024-08-06",
model="gpt-6.2-sol",
messages=[
{"role": "system", "content": "You are a helpful math tutor."},
{"role": "user", "content": "solve 8x + 31 = 2"},
Expand Down
2 changes: 1 addition & 1 deletion examples/parsing_stream.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class MathResponse(BaseModel):
client = OpenAI()

with client.chat.completions.stream(
model="gpt-4o-2024-08-06",
model="gpt-6.2-sol",
messages=[
{"role": "system", "content": "You are a helpful math tutor."},
{"role": "user", "content": "solve 8x + 31 = 2"},
Expand Down
2 changes: 1 addition & 1 deletion examples/responses/background.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class MathResponse(BaseModel):

with client.responses.create(
input="solve 8x + 31 = 2",
model="gpt-4o-2024-08-06",
model="gpt-6.2-sol",
background=True,
stream=True,
) as stream:
Expand Down
2 changes: 1 addition & 1 deletion examples/responses/background_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ async def main() -> None:

async with await client.responses.create(
input="solve 8x + 31 = 2",
model="gpt-4o-2024-08-06",
model="gpt-6.2-sol",
background=True,
stream=True,
) as stream:
Expand Down
2 changes: 1 addition & 1 deletion examples/responses/background_streaming.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class MathResponse(BaseModel):
id = None
with client.responses.stream(
input="solve 8x + 31 = 2",
model="gpt-4o-2024-08-06",
model="gpt-6.2-sol",
text_format=MathResponse,
background=True,
) as stream:
Expand Down
Loading
Loading