Skip to content

Conversation

hsliuustc0106
Copy link
Contributor

@hsliuustc0106 hsliuustc0106 commented Jul 29, 2025

Essential Elements of an Effective PR Description Checklist

  • The purpose of the PR, such as "Fix some issue (link existing issues this PR will resolve)".
  • The test plan, such as providing test command.
  • The test results, such as pasting the results comparison before and after, or e2e results
  • (Optional) The necessary documentation update, such as updating supported_models.md and examples for a new model.

Purpose

Fix the example code in reasoning_outputs.md described in #21736

Test Plan

serve command:

vllm serve /workspace/models/granite-3.2-8b-instruct     --reasoning-parser granite --served-model-name ' ibm-granite/granite-3.2-8b-instruct'

code:

from openai import OpenAI

# Modify OpenAI's API key and API base to use vLLM's API server.
openai_api_key = "EMPTY"
openai_api_base = "http://localhost:8000/v1"

messages = [{"role": "user", "content": "9.11 and 9.8, which is greater?"}]


def main():
    client = OpenAI(
        api_key=openai_api_key,
        base_url=openai_api_base,
    )

    models = client.models.list()
    model = models.data[0].id

    # ruff: noqa: E501
    # For granite: add: `extra_body={"chat_template_kwargs": {"thinking": True}}`
    stream = client.chat.completions.create(model=model, messages=messages, stream=True, extra_body={"chat_template_kwargs": {"thinking": True}})

    print("client: Start streaming chat completions...")
    printed_reasoning_content = False
    printed_content = False

    for chunk in stream:
        reasoning_content = None
        content = None
        # Check the content is reasoning_content or content
        if (
            hasattr(chunk.choices[0].delta, "reasoning_content")
            and chunk.choices[0].delta.reasoning_content
        ):
            reasoning_content = chunk.choices[0].delta.reasoning_content
        elif (
            hasattr(chunk.choices[0].delta, "content")
            and chunk.choices[0].delta.content
        ):
            content = chunk.choices[0].delta.content

        if reasoning_content is not None:
            if not printed_reasoning_content:
                printed_reasoning_content = True
                print("reasoning_content:", end="", flush=True)
            print(reasoning_content, end="", flush=True)
        elif content is not None:
            if not printed_content:
                printed_content = True
                print("\ncontent:", end="", flush=True)
            # Extract and print the content
            print(content, end="", flush=True)


if __name__ == "__main__":
    main()

Test Result

output:

client: Start streaming chat completions...
reasoning_content:
This question is asking to compare two specific numbers, 9.11 and 9.8. It's a simple numerical comparison that involves understanding decimal places. 

1. Recognize that both numbers are close but 9.8 has a higher whole number part (9) than 9.11.
2. Consider the decimal places: 9.11 is closer to 9 but has a slightly larger fractional part.
3. Conclude that 9.8 is greater because its whole number component (9) is greater than 9.11's (9), and the decimal portion doesn't change this. 


content:

9.8 is greater than 9.11. 

Here’s the reasoning:

1. **Whole Number Comparison**: Both numbers start with '9', but 9.8 has a whole number component of 9, whereas 9.11 has 9.1. 
2. Since the whole numbers are equal, we look to the decimal parts. 
3. 9.8's decimal part is .8, and 9.11's is .11. 
4. Any number with a higher decimal value is greater than a number with a lower decimal value, even when the whole numbers are the same. 

Therefore, 9.8 > 9.11.

(Optional) Documentation Update

Update the example code of streaming chat completions in reasoning_outputs.md

BruceW-07 and others added 2 commits July 29, 2025 11:01
[Docs] Fix the example code of streaming chat completions in reasonin…
@hsliuustc0106 hsliuustc0106 requested a review from hmellor as a code owner July 29, 2025 10:58
@mergify mergify bot added the documentation Improvements or additions to documentation label Jul 29, 2025
Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request correctly fixes a bug in the example code for streaming chat completions with reasoning, where empty reasoning_content could cause the actual content to be missed. The proposed change is logically sound and addresses the issue.

Comment on lines 129 to 131
if (
hasattr(chunk.choices[0].delta, "reasoning_content")
and chunk.choices[0].delta.reasoning_content
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

The added checks for chunk.choices[0].delta.reasoning_content and chunk.choices[0].delta.content are necessary to avoid errors when these attributes are not present in the response. This ensures the code handles different response formats gracefully.

Suggested change
if (
hasattr(chunk.choices[0].delta, "reasoning_content")
and chunk.choices[0].delta.reasoning_content
if (
hasattr(chunk.choices[0].delta, "reasoning_content")
and chunk.choices[0].delta.reasoning_content
):

Copy link

👋 Hi! Thank you for contributing to the vLLM project.

💬 Join our developer Slack at https://slack.vllm.ai to discuss your PR in #pr-reviews, coordinate on features in #feat- channels, or join special interest groups in #sig- channels.

Just a reminder: PRs would not trigger full CI run by default. Instead, it would only run fastcheck CI which starts running only a small and essential subset of CI tests to quickly catch errors. You can run other CI tests on top of those by going to your fastcheck build on Buildkite UI (linked in the PR checks section) and unblock them. If you do not have permission to unblock, ping simon-mo or khluu to add you in our Buildkite org.

Once the PR is approved and ready to go, your PR reviewer(s) can run CI to test the changes comprehensively before merging.

To run CI, PR reviewers can either: Add ready label to the PR or enable auto-merge.

🚀

Comment on lines 54 to 65
reasoning_content = None
content = None
# Check the content is reasoning_content or content
if hasattr(chunk.choices[0].delta, "reasoning_content"):
if (
hasattr(chunk.choices[0].delta, "reasoning_content")
and chunk.choices[0].delta.reasoning_content
):
reasoning_content = chunk.choices[0].delta.reasoning_content
elif hasattr(chunk.choices[0].delta, "content"):
elif (
hasattr(chunk.choices[0].delta, "content")
and chunk.choices[0].delta.content
):
Copy link
Member

@hmellor hmellor Jul 29, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
reasoning_content = None
content = None
# Check the content is reasoning_content or content
if hasattr(chunk.choices[0].delta, "reasoning_content"):
if (
hasattr(chunk.choices[0].delta, "reasoning_content")
and chunk.choices[0].delta.reasoning_content
):
reasoning_content = chunk.choices[0].delta.reasoning_content
elif hasattr(chunk.choices[0].delta, "content"):
elif (
hasattr(chunk.choices[0].delta, "content")
and chunk.choices[0].delta.content
):
reasoning_content = getattr(chunk.choices[0].delta, "reasoning_content", None) or None
content = getattr(chunk.choices[0].delta, "content", None) or None

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the advice! The code has been simplified.

BruceW-07 and others added 3 commits July 30, 2025 10:15
Signed-off-by: wangzi <[email protected]>
[Docs] Fix the example code of streaming chat completions in reasoning_outputs.md
Copy link
Member

@hmellor hmellor left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks for the improvement!

@hmellor hmellor enabled auto-merge (squash) July 30, 2025 08:01
@github-actions github-actions bot added the ready ONLY add when PR is ready to merge/full CI is needed label Jul 30, 2025
@hmellor hmellor merged commit 5c8fe38 into vllm-project:main Jul 30, 2025
54 checks passed
liuyumoye pushed a commit to liuyumoye/vllm that referenced this pull request Jul 31, 2025
juuice-lee pushed a commit to juuice-lee/vllm-moe.code that referenced this pull request Jul 31, 2025
wenscarl pushed a commit to wenscarl/vllm that referenced this pull request Aug 4, 2025
wenscarl pushed a commit to wenscarl/vllm that referenced this pull request Aug 4, 2025
vadiklyutiy pushed a commit to CentML/vllm that referenced this pull request Aug 5, 2025
x22x22 pushed a commit to x22x22/vllm that referenced this pull request Aug 5, 2025
x22x22 pushed a commit to x22x22/vllm that referenced this pull request Aug 5, 2025
npanpaliya pushed a commit to odh-on-pz/vllm-upstream that referenced this pull request Aug 6, 2025
jinzhen-lin pushed a commit to jinzhen-lin/vllm that referenced this pull request Aug 9, 2025
noamgat pushed a commit to noamgat/vllm that referenced this pull request Aug 9, 2025
paulpak58 pushed a commit to paulpak58/vllm that referenced this pull request Aug 13, 2025
taneem-ibrahim pushed a commit to taneem-ibrahim/vllm that referenced this pull request Aug 14, 2025
BoyuanFeng pushed a commit to BoyuanFeng/vllm that referenced this pull request Aug 14, 2025
diegocastanibm pushed a commit to diegocastanibm/vllm that referenced this pull request Aug 15, 2025
vllm-project#21825)

Signed-off-by: wangzi <[email protected]>
Co-authored-by: wangzi <[email protected]>
Co-authored-by: Zi Wang <[email protected]>
Signed-off-by: Diego-Castan <[email protected]>
epwalsh pushed a commit to epwalsh/vllm that referenced this pull request Aug 28, 2025
zhewenl pushed a commit to zhewenl/vllm that referenced this pull request Aug 28, 2025
googlercolin pushed a commit to googlercolin/vllm that referenced this pull request Aug 29, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation ready ONLY add when PR is ready to merge/full CI is needed
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants