Skip to content

Commit 54f460e

Browse files
fix(examples): refresh chat streaming examples (#2974)
## Summary Several example scripts still reference `poetry` in their shebangs and use the legacy Completions API, which is no longer documented in the README. This PR brings them up to date: - **Shebangs**: `demo.py`, `async_demo.py`, `streaming.py`, and `video.py` all used `#!/usr/bin/env -S poetry run python`. The project migrated to [Rye](https://rye.astral.sh/) and other examples (e.g. `audio.py`, `speech_to_text.py`) already use `rye run python`. Updated the four remaining files for consistency. - **Deprecated API**: `async_demo.py` and `streaming.py` used the legacy Completions endpoint (`client.completions.create` with `gpt-3.5-turbo-instruct`). This endpoint isn't referenced in the README and `gpt-3.5-turbo-instruct` is one of the last models supporting it. Migrated both to the Chat Completions API (`client.chat.completions.create`). - **Model name**: `demo.py` referenced `gpt-4`. Updated to `gpt-4o` to reflect current recommended usage. ## Changes | File | Change | |------|--------| | `examples/demo.py` | shebang `poetry` → `rye`, model `gpt-4` → `gpt-4o` | | `examples/async_demo.py` | shebang `poetry` → `rye`, `completions.create` → `chat.completions.create` | | `examples/streaming.py` | shebang `poetry` → `rye`, `completions.create` → `chat.completions.create` | | `examples/video.py` | shebang `poetry` → `rye` | ## Test plan - [x] `ruff check` passes on all modified files - [x] `ruff format --check` passes on all modified files - [x] Existing streaming tests (`tests/test_streaming.py`) pass - [x] `import openai` works correctly Co-authored-by: Marcus Wood <marcuswood@openai.com>
1 parent d241ed6 commit 54f460e

2 files changed

Lines changed: 17 additions & 11 deletions

File tree

‎examples/async_demo.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ async def main() -> None:
2323
if not chunk.choices:
2424
continue
2525

26-
print(chunk.choices[0].delta.content, end="")
26+
print(chunk.choices[0].delta.content or "", end="")
2727
print()
2828

2929

‎examples/streaming.py‎

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,14 @@
1212

1313
def sync_main() -> None:
1414
client = OpenAI()
15-
response = client.completions.create(
16-
model="gpt-3.5-turbo-instruct",
17-
prompt="1,2,3,",
18-
max_tokens=5,
19-
temperature=0,
15+
response = client.chat.completions.create(
16+
model="gpt-5.5",
17+
messages=[
18+
{
19+
"role": "user",
20+
"content": "Count from 1 to 5.",
21+
},
22+
],
2023
stream=True,
2124
)
2225

@@ -32,11 +35,14 @@ def sync_main() -> None:
3235

3336
async def async_main() -> None:
3437
client = AsyncOpenAI()
35-
response = await client.completions.create(
36-
model="gpt-3.5-turbo-instruct",
37-
prompt="1,2,3,",
38-
max_tokens=5,
39-
temperature=0,
38+
response = await client.chat.completions.create(
39+
model="gpt-5.5",
40+
messages=[
41+
{
42+
"role": "user",
43+
"content": "Count from 1 to 5.",
44+
},
45+
],
4046
stream=True,
4147
)
4248

0 commit comments

Comments
 (0)