From 6f553cd43eb49105c5de78ec48a7613b71e36eb5 Mon Sep 17 00:00:00 2001 From: garjarbinks Date: Tue, 24 Nov 2026 16:24:10 -0500 Subject: [PATCH] Build-Things Tutorial - response message content index 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. --- .../building-a-chatbot-with-cohere.mdx | 7 ++-- .../building-an-agent-with-cohere.mdx | 11 +++--- .../rag-with-cohere.mdx | 4 +-- .../text-generation-tutorial.mdx | 21 +++++++----- .../getting-started/v2/tutorial_pt2_v2.ipynb | 34 +++++++++---------- .../getting-started/v2/tutorial_pt3_v2.ipynb | 24 ++++++------- .../getting-started/v2/tutorial_pt6_v2.ipynb | 4 +-- .../getting-started/v2/tutorial_pt7_v2.ipynb | 4 +-- 8 files changed, 59 insertions(+), 50 deletions(-) diff --git a/fern/pages/v2/tutorials/build-things-with-cohere/building-a-chatbot-with-cohere.mdx b/fern/pages/v2/tutorials/build-things-with-cohere/building-a-chatbot-with-cohere.mdx index 75852f7f..a9c87b34 100644 --- a/fern/pages/v2/tutorials/build-things-with-cohere/building-a-chatbot-with-cohere.mdx +++ b/fern/pages/v2/tutorials/build-things-with-cohere/building-a-chatbot-with-cohere.mdx @@ -95,8 +95,11 @@ response = co.chat( }, ], ) +print(response.message.content[-1].text) -print(response.message.content[0].text) +# for message in response.message.content: +# if hasattr(message, 'text'): +# print(message.text) ``` ``` @@ -158,7 +161,7 @@ response = co.chat( # assistant turn 2 messages.append(response.message) -print(response.message.content[0].text) +print(response.message.content[-1].text) ``` diff --git a/fern/pages/v2/tutorials/build-things-with-cohere/building-an-agent-with-cohere.mdx b/fern/pages/v2/tutorials/build-things-with-cohere/building-an-agent-with-cohere.mdx index c2c928cf..4e585439 100644 --- a/fern/pages/v2/tutorials/build-things-with-cohere/building-an-agent-with-cohere.mdx +++ b/fern/pages/v2/tutorials/build-things-with-cohere/building-an-agent-with-cohere.mdx @@ -285,12 +285,15 @@ response = co.chat( # Append assistant response to the chat history messages.append( - {"role": "assistant", "content": response.message.content[0].text} + { + "role": "assistant", + "content": response.message.content[-1].text, + } ) # Print final response print("Response:") -print(response.message.content[0].text) +print(response.message.content[-1].text) print("=" * 50) # Print citations (if any) @@ -385,13 +388,13 @@ def run_assistant(query, messages=None): messages.append( { "role": "assistant", - "content": response.message.content[0].text, + "content": response.message.content[-1].text, } ) # Print final response print("Response:") - print(response.message.content[0].text) + print(response.message.content[-1].text) print("=" * 50) # Print citations (if any) diff --git a/fern/pages/v2/tutorials/build-things-with-cohere/rag-with-cohere.mdx b/fern/pages/v2/tutorials/build-things-with-cohere/rag-with-cohere.mdx index c15707ce..05b0a929 100644 --- a/fern/pages/v2/tutorials/build-things-with-cohere/rag-with-cohere.mdx +++ b/fern/pages/v2/tutorials/build-things-with-cohere/rag-with-cohere.mdx @@ -98,7 +98,7 @@ response = co.chat( ) # Display the response -print(response.message.content[0].text) +print(response.message.content[-1].text) # Display the citations and source documents if response.message.citations: @@ -436,7 +436,7 @@ response = co.chat( ) # Display the response -print(response.message.content[0].text) +print(response.message.content[-1].text) # Display the citations and source documents if response.message.citations: diff --git a/fern/pages/v2/tutorials/build-things-with-cohere/text-generation-tutorial.mdx b/fern/pages/v2/tutorials/build-things-with-cohere/text-generation-tutorial.mdx index 09916006..f78b3146 100644 --- a/fern/pages/v2/tutorials/build-things-with-cohere/text-generation-tutorial.mdx +++ b/fern/pages/v2/tutorials/build-things-with-cohere/text-generation-tutorial.mdx @@ -44,7 +44,7 @@ co = cohere.ClientV2(api_key="COHERE_API_KEY") To get started with Chat, we need to pass two parameters, `model` for the LLM model ID and `messages`, which we add a single user message. We then call the Chat endpoint through the client we created earlier. -The response contains several objects. For simplicity, what we want right now is the `message.content[0].text` object. +The response contains several objects. For simplicity, what we want right now is the `message.content[-1].text` object (the `text` attribute of the last (-1) element of the `content` array attribute). Here's an example of the assistant responding to a new hire's query asking for help to make introductions. @@ -60,7 +60,7 @@ response = co.chat( ) # messages=[cohere.UserMessage(content=message)]) -print(response.message.content[0].text) +print(response.message.content[-1].text) ``` ``` @@ -103,7 +103,7 @@ response = co.chat( ) # messages=[cohere.UserMessage(content=message)]) -print(response.message.content[0].text) +print(response.message.content[-1].text) ``` ``` @@ -144,7 +144,7 @@ response = co.chat( messages=[{"role": "user", "content": message}], ) -print(response.message.content[0].text) +print(response.message.content[-1].text) ``` ``` @@ -173,7 +173,7 @@ response = co.chat( messages=[{"role": "user", "content": message}], ) -print(response.message.content[0].text) +print(response.message.content[-1].text) ``` ``` @@ -201,7 +201,7 @@ for idx in range(3): temperature=0, ) - print(f"{idx+1}: {response.message.content[0].text}\n") + print(f"{idx+1}: {response.message.content[-1].text}\n") ``` ``` @@ -228,7 +228,7 @@ for idx in range(3): temperature=1, ) - print(f"{idx+1}: {response.message.content[0].text}\n") + print(f"{idx+1}: {response.message.content[-1].text}\n") ``` ``` @@ -293,7 +293,7 @@ response = co.chat( }, ) -json_object = json.loads(response.message.content[0].text) +json_object = json.loads(response.message.content[-1].text) print(json_object) ``` @@ -329,7 +329,10 @@ response = co.chat_stream( for event in response: if event: - if event.type == "content-delta": + if ( + event.type == "content-delta" + and event.delta.message.content.text is not None + ): print(event.delta.message.content.text, end="") ``` diff --git a/notebooks/guides/getting-started/v2/tutorial_pt2_v2.ipynb b/notebooks/guides/getting-started/v2/tutorial_pt2_v2.ipynb index c6e342a0..7143af4d 100644 --- a/notebooks/guides/getting-started/v2/tutorial_pt2_v2.ipynb +++ b/notebooks/guides/getting-started/v2/tutorial_pt2_v2.ipynb @@ -72,14 +72,14 @@ "source": [ "To get started with Chat, we need to pass two parameters, `model` for the LLM model ID and `messages`, which we add a single user message. We then call the Chat endpoint through the client we created earlier.\n", "\n", - "The response contains several objects. For simplicity, what we want right now is the `message.content[0].text` object.\n", + "The response contains several objects. For simplicity, what we want right now is the `message.content[-1].text` object (the `text` attribute of the last (-1) element of the `content` array attribute).\n", "\n", "Here's an example of the assistant responding to a new hire's query asking for help to make introductions." ] }, { "cell_type": "code", - "execution_count": 4, + "execution_count": null, "metadata": {}, "outputs": [ { @@ -124,7 +124,7 @@ ")\n", "# messages=[cohere.UserMessage(content=message)])\n", "\n", - "print(response.message.content[0].text)" + "print(response.message.content[-1].text)" ] }, { @@ -162,7 +162,7 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": null, "metadata": {}, "outputs": [ { @@ -183,7 +183,7 @@ " messages=[{\"role\": \"user\", \"content\": message}]\n", ")\n", "\n", - "print(response.message.content[0].text)" + "print(response.message.content[-1].text)" ] }, { @@ -199,7 +199,7 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": null, "metadata": {}, "outputs": [ { @@ -235,7 +235,7 @@ " messages=[{\"role\": \"user\", \"content\": message}]\n", ")\n", "\n", - "print(response.message.content[0].text)" + "print(response.message.content[-1].text)" ] }, { @@ -265,7 +265,7 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": null, "metadata": {}, "outputs": [ { @@ -284,7 +284,7 @@ "response = co.chat(model=\"command-a-plus-05-2026\",\n", " messages=[{\"role\": \"user\", \"content\": message}])\n", "\n", - "print(response.message.content[0].text)" + "print(response.message.content[-1].text)" ] }, { @@ -302,7 +302,7 @@ }, { "cell_type": "code", - "execution_count": 8, + "execution_count": null, "metadata": {}, "outputs": [ { @@ -330,7 +330,7 @@ " temperature=0,\n", " )\n", "\n", - " print(f\"{idx+1}: {response.message.content[0].text}\\n\")" + " print(f\"{idx+1}: {response.message.content[-1].text}\\n\")" ] }, { @@ -342,7 +342,7 @@ }, { "cell_type": "code", - "execution_count": 11, + "execution_count": null, "metadata": {}, "outputs": [ { @@ -370,7 +370,7 @@ " temperature=1,\n", " )\n", "\n", - " print(f\"{idx+1}: {response.message.content[0].text}\\n\")" + " print(f\"{idx+1}: {response.message.content[-1].text}\\n\")" ] }, { @@ -403,7 +403,7 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": null, "metadata": {}, "outputs": [ { @@ -446,7 +446,7 @@ " },\n", ")\n", "\n", - "json_object = json.loads(response.message.content[0].text)\n", + "json_object = json.loads(response.message.content[-1].text)\n", "\n", "print(json_object)" ] @@ -481,7 +481,7 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": null, "metadata": {}, "outputs": [ { @@ -502,7 +502,7 @@ "\n", "for event in response:\n", " if event:\n", - " if event.type == \"content-delta\":\n", + " if event.type == \"content-delta\" and event.delta.message.content.text is not None:\n", " print(event.delta.message.content.text, end=\"\")" ] }, diff --git a/notebooks/guides/getting-started/v2/tutorial_pt3_v2.ipynb b/notebooks/guides/getting-started/v2/tutorial_pt3_v2.ipynb index 017c386b..b8d61a76 100644 --- a/notebooks/guides/getting-started/v2/tutorial_pt3_v2.ipynb +++ b/notebooks/guides/getting-started/v2/tutorial_pt3_v2.ipynb @@ -76,7 +76,7 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": null, "metadata": {}, "outputs": [ { @@ -119,7 +119,7 @@ "# Generate the response\n", "response = co.chat(model=\"command-a-plus-05-2026\", messages=messages)\n", "\n", - "print(response.message.content[0].text)" + "print(response.message.content[-1].text)" ] }, { @@ -148,7 +148,7 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": null, "metadata": {}, "outputs": [ { @@ -175,7 +175,7 @@ "response = co.chat(model=\"command-a-plus-05-2026\",\n", " messages=messages)\n", "\n", - "print(response.message.content[0].text)" + "print(response.message.content[-1].text)" ] }, { @@ -200,7 +200,7 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": null, "metadata": {}, "outputs": [ { @@ -213,7 +213,7 @@ ], "source": [ "# Append the previous response\n", - "messages.append({'role' : 'assistant', 'content': response.message.content[0].text})\n", + "messages.append({'role' : 'assistant', 'content': response.message.content[-1].text})\n", "\n", "# Add the user message\n", "message = \"Make it more upbeat and conversational.\"\n", @@ -225,7 +225,7 @@ "response = co.chat(model=\"command-a-plus-05-2026\",\n", " messages=messages)\n", "\n", - "print(response.message.content[0].text)" + "print(response.message.content[-1].text)" ] }, { @@ -253,7 +253,7 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": null, "metadata": {}, "outputs": [ { @@ -266,7 +266,7 @@ ], "source": [ "# Append the previous response\n", - "messages.append({\"role\": \"assistant\", \"content\": response.message.content[0].text})\n", + "messages.append({\"role\": \"assistant\", \"content\": response.message.content[-1].text})\n", "\n", "# Add the user message\n", "message = \"Thanks. Could you create another one for my DM to my manager.\"\n", @@ -278,7 +278,7 @@ "response = co.chat(model=\"command-a-plus-05-2026\",\n", " messages=messages)\n", "\n", - "print(response.message.content[0].text)" + "print(response.message.content[-1].text)" ] }, { @@ -297,7 +297,7 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": null, "metadata": {}, "outputs": [ { @@ -323,7 +323,7 @@ ], "source": [ "# Append the previous response\n", - "messages.append({\"role\": \"assistant\", \"content\": response.message.content[0].text})\n", + "messages.append({\"role\": \"assistant\", \"content\": response.message.content[-1].text})\n", "\n", "# View the chat history\n", "for message in messages:\n", diff --git a/notebooks/guides/getting-started/v2/tutorial_pt6_v2.ipynb b/notebooks/guides/getting-started/v2/tutorial_pt6_v2.ipynb index 1217accf..76bc647e 100644 --- a/notebooks/guides/getting-started/v2/tutorial_pt6_v2.ipynb +++ b/notebooks/guides/getting-started/v2/tutorial_pt6_v2.ipynb @@ -157,7 +157,7 @@ ")\n", "\n", "# Display the response\n", - "print(response.message.content[0].text)\n", + "print(response.message.content[-1].text)\n", "\n", "# Display the citations and source documents\n", "if response.message.citations:\n", @@ -618,7 +618,7 @@ ")\n", "\n", "# Display the response\n", - "print(response.message.content[0].text)\n", + "print(response.message.content[-1].text)\n", "\n", "# Display the citations and source documents\n", "if response.message.citations:\n", diff --git a/notebooks/guides/getting-started/v2/tutorial_pt7_v2.ipynb b/notebooks/guides/getting-started/v2/tutorial_pt7_v2.ipynb index 7ffe6255..448832d6 100644 --- a/notebooks/guides/getting-started/v2/tutorial_pt7_v2.ipynb +++ b/notebooks/guides/getting-started/v2/tutorial_pt7_v2.ipynb @@ -379,7 +379,7 @@ "\n", "# Print final response\n", "print(\"Response:\")\n", - "print(response.message.content[0].text)\n", + "print(response.message.content[-1].text)\n", "print(\"=\" * 50)\n", "\n", "# Print citations (if any)\n", @@ -468,7 +468,7 @@ " step += 1\n", " else: \n", " print(\"RESPONSE:\")\n", - " print(response.message.content[0].text)\n", + " print(response.message.content[-1].text)\n", "\n", " # Print citations (if any)\n", " if response.message.citations:\n",