Skip to content

Build-Things Tutorial - response message content index - #845

Open
garjarbinks wants to merge 1 commit into
cohere-ai:mainfrom
garjarbinks:fix/build-things-tutorial-response-extraction
Open

garjarbinks wants to merge 1 commit into
cohere-ai:mainfrom
garjarbinks:fix/build-things-tutorial-response-extraction

Conversation

@garjarbinks

@garjarbinks garjarbinks commented Nov 24, 2026 •

Copy link
Copy Markdown

Problem

Several Python examples access the generated response using response.message.content[0].text.

For non-reasoning models, the first content block typically contains the generated text. However, reasoning models can return a thinking block before the final text block.

This causes existing examples to fail or incorrectly access the reasoning content.

Proposed change

Replace response.message.content[0].text with response.message.content[-1].text where the final content block contains the generated response.

Although filtering the content array by content type would be more robust, accessing the final content block keeps introductory code examples simple while supporting both reasoning and non-reasoning models.

Scope

Documentation and Python code examples, including notebooks, related only to the "Build Things with Cohere" turorial

fern/pages/v2/tutorials/build-things-with-cohere
notebooks/guides/getting-started/v2


Note

Low Risk
Documentation and notebook example updates only; no application or API runtime code changes.

Overview
Updates Build Things with Cohere docs and matching v2 notebooks so examples read assistant output from response.message.content[-1].text instead of content[0].text, so copied code still works when models return a reasoning/thinking block before the final text.

The text-generation tutorial prose now describes that last-content-block pattern. Streaming examples only print content-delta tokens when event.delta.message.content.text is not None. The chatbot tutorial adds an optional commented loop for iterating content blocks.

Reviewed by Cursor Bugbot for commit 6f553cd. Bugbot is set up for automated code reviews on this repo. Configure here.

changed response.message.content[0].text to response.message.content[-1].text.

In non-thinking/non-reasoning models index 0 is what we want, but in thinking/reasoning models, content index 0 is the "thoughts".

Ideally we'd enumerate the content array for responses of the correct type, but to keep the code super-simple, I'd use the LAST index of the content array, vs that 0th.

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes using default effort and found 1 potential issue.

Fix All in Cursor

Reviewed by Cursor Bugbot for commit 6f553cd. Configure here.

print(response.message.content[0].text)
# for message in response.message.content:
# if hasattr(message, 'text'):
# print(message.text)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Leftover commented-out tutorial code

Low Severity

A commented-out loop over response.message.content was left in the first chatbot example after the content[-1].text print. It has no surrounding explanation, appears in only this snippet, and looks like an exploratory alternative rather than tutorial content.

Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit 6f553cd. Configure here.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Could add a comment description if necessary. Just wanted to show an example for the user of iterating the messages for only those that have a text attribute.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant