Skip to content

Commit 90eeb62

Browse files
hsliuustc0106BruceW-07
authored andcommitted
[Docs] Fix the example code of streaming chat completions in reasoning (vllm-project#21825)
Signed-off-by: wangzi <[email protected]> Co-authored-by: wangzi <[email protected]> Co-authored-by: Zi Wang <[email protected]>
1 parent bbe08a3 commit 90eeb62

File tree

2 files changed

+12
-14
lines changed

2 files changed

+12
-14
lines changed

docs/features/reasoning_outputs.md

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -123,13 +123,12 @@ OpenAI Python client library does not officially support `reasoning_content` att
123123
printed_content = False
124124

125125
for chunk in stream:
126-
reasoning_content = None
127-
content = None
128-
# Check the content is reasoning_content or content
129-
if hasattr(chunk.choices[0].delta, "reasoning_content"):
130-
reasoning_content = chunk.choices[0].delta.reasoning_content
131-
elif hasattr(chunk.choices[0].delta, "content"):
132-
content = chunk.choices[0].delta.content
126+
# Safely extract reasoning_content and content from delta,
127+
# defaulting to None if attributes don't exist or are empty strings
128+
reasoning_content = (
129+
getattr(chunk.choices[0].delta, "reasoning_content", None) or None
130+
)
131+
content = getattr(chunk.choices[0].delta, "content", None) or None
133132

134133
if reasoning_content is not None:
135134
if not printed_reasoning_content:

examples/online_serving/openai_chat_completion_with_reasoning_streaming.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,12 @@ def main():
5151
printed_content = False
5252

5353
for chunk in stream:
54-
reasoning_content = None
55-
content = None
56-
# Check the content is reasoning_content or content
57-
if hasattr(chunk.choices[0].delta, "reasoning_content"):
58-
reasoning_content = chunk.choices[0].delta.reasoning_content
59-
elif hasattr(chunk.choices[0].delta, "content"):
60-
content = chunk.choices[0].delta.content
54+
# Safely extract reasoning_content and content from delta,
55+
# defaulting to None if attributes don't exist or are empty strings
56+
reasoning_content = (
57+
getattr(chunk.choices[0].delta, "reasoning_content", None) or None
58+
)
59+
content = getattr(chunk.choices[0].delta, "content", None) or None
6160

6261
if reasoning_content is not None:
6362
if not printed_reasoning_content:

0 commit comments

Comments
 (0)