Fix response content handling in text generation tutorial - #798
Fix response content handling in text generation tutorial#798Rishabh-k-Paliwal wants to merge 2 commits into
Conversation
|
I ran into the same error just now and happy that you picked it up, thanks! One thing that's still left: the streaming example at the bottom of the same file has the same problem, just in a different shape. Your last hunk stops at the structured output snippet, so this one is untouched: for event in response:
if event:
if event.type == "content-delta":
print(event.delta.message.content.text, end="")Thinking blocks fire Something like this fixes it: for event in response:
if event and event.type == "content-delta":
text = event.delta.message.content.text
if text:
print(text, end="", flush=True)The Small one in the same section: the text above that snippet still says Happy to push a commit to your branch or send a follow up PR after this merges, whatever works. Didn't want to open a competing PR on the same file. |
|
@LouayYa Thanks for catching this and for the detailed reproduction. I've updated the streaming example to avoid printing None for thinking chunks, added flush=True for incremental output, and corrected event_type to event.type. The changes are now pushed to the PR for review. Appreciate you pointing this out! |
Summary
This PR fixes additional occurrences of direct
response.message.content[0].textaccess in the text generation tutorial.This follows the issue discussed in #796, where
message.content[0]could contain athinkingblock rather than atextblock. The initial snippets were addressed in #797, but I found additional examples in the tutorial with the same assumption.Changes
response.message.content[0].textaccess where appropriate.Related
Related to #796.
Note
Low Risk
Documentation-only changes to tutorial code samples; no runtime or API behavior.
Overview
Updates the v2 text generation tutorial so sample code no longer assumes the first
message.contententry is always text—matching the fix from #797 for the rest of the doc (#796).Non-streaming examples now iterate
response.message.contentand branch ontype(thinkingvstext) instead of usingresponse.message.content[0].text. Temperature and structured-output snippets only read text items (JSON parsing uses the first text block). The streaming section is aligned with current event shape: prose refers toevent.type, and the loop printscontent-deltadeltas with a guard andflush=True.A short note that pointed readers only at
message.content[0].textwas removed.Reviewed by Cursor Bugbot for commit 96b3cc4. Bugbot is set up for automated code reviews on this repo. Configure here.