openai/openai-python

Public

mirrored from https://github.com/openai/openai-pythonAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
v1.109.0

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

examples/responses/streaming.py

30lines · modeblame

2954945eRobert Craigie1 years ago1from typing import List
2
3import rich
4from pydantic import BaseModel
5
6from openai import OpenAI
7
8
9class Step(BaseModel):
10explanation: str
11output: str
12
13
14class MathResponse(BaseModel):
15steps: List[Step]
16final_answer: str
17
18
19client = OpenAI()
20
21with client.responses.stream(
22input="solve 8x + 31 = 2",
23model="gpt-4o-2024-08-06",
24text_format=MathResponse,
25) as stream:
26for event in stream:
27if "output_text" in event.type:
28rich.print(event)
29
30rich.print(stream.get_final_response())