From 05ea4618b7f67441b82c72de23c3bf45d3ad03b0 Mon Sep 17 00:00:00 2001 From: Rishabh Paliwal Date: Tue, 25 Aug 2026 23:40:47 +0530 Subject: [PATCH 1/2] Fix response content handling in text generation tutorial --- .../text-generation-tutorial.mdx | 39 ++++++++++++++----- 1 file changed, 30 insertions(+), 9 deletions(-) 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 099160065..37ab23ba6 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,8 +44,6 @@ 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. - Here's an example of the assistant responding to a new hire's query asking for help to make introductions. @@ -60,7 +58,11 @@ response = co.chat( ) # messages=[cohere.UserMessage(content=message)]) -print(response.message.content[0].text) +for content_item in response.message.content: + if content_item.type == "thinking": + print("thinking:", content_item.thinking) + if content_item.type == "text": + print("text:", content_item.text) ``` ``` @@ -103,7 +105,11 @@ response = co.chat( ) # messages=[cohere.UserMessage(content=message)]) -print(response.message.content[0].text) +for content_item in response.message.content: + if content_item.type == "thinking": + print("thinking:", content_item.thinking) + if content_item.type == "text": + print("text:", content_item.text) ``` ``` @@ -144,7 +150,11 @@ response = co.chat( messages=[{"role": "user", "content": message}], ) -print(response.message.content[0].text) +for content_item in response.message.content: + if content_item.type == "thinking": + print("thinking:", content_item.thinking) + if content_item.type == "text": + print("text:", content_item.text) ``` ``` @@ -173,7 +183,11 @@ response = co.chat( messages=[{"role": "user", "content": message}], ) -print(response.message.content[0].text) +for content_item in response.message.content: + if content_item.type == "thinking": + print("thinking:", content_item.thinking) + if content_item.type == "text": + print("text:", content_item.text) ``` ``` @@ -201,7 +215,9 @@ for idx in range(3): temperature=0, ) - print(f"{idx+1}: {response.message.content[0].text}\n") + for content_item in response.message.content: + if content_item.type == "text": + print(f"{idx+1}: {content_item.text}\n") ``` ``` @@ -228,7 +244,9 @@ for idx in range(3): temperature=1, ) - print(f"{idx+1}: {response.message.content[0].text}\n") + for content_item in response.message.content: + if content_item.type == "text": + print(f"{idx+1}: {content_item.text}\n") ``` ``` @@ -293,7 +311,10 @@ response = co.chat( }, ) -json_object = json.loads(response.message.content[0].text) +for content_item in response.message.content: + if content_item.type == "text": + json_object = json.loads(content_item.text) + break print(json_object) ``` From 96b3cc491444642e7672148da4fb223df1e57da1 Mon Sep 17 00:00:00 2001 From: Rishabh Paliwal Date: Sun, 30 Aug 2026 11:11:51 +0530 Subject: [PATCH 2/2] Fix streaming response content handling --- .../build-things-with-cohere/text-generation-tutorial.mdx | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) 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 37ab23ba6..bd8e33e53 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 @@ -335,7 +335,7 @@ The Chat endpoint also provides streaming support. In a streamed response, the e To activate it, use `co.chat_stream()` instead of `co.chat()`. -In streaming mode, the endpoint will generate a series of objects. To get the actual text contents, we take objects whose `event_type` is `content-delta`. +In streaming mode, the endpoint will generate a series of objects. To get the actual text contents, we take objects whose `event.type` is `content-delta`. ```python PYTHON @@ -349,9 +349,9 @@ response = co.chat_stream( ) for event in response: - if event: - if event.type == "content-delta": - print(event.delta.message.content.text, end="") + if event and event.type == "content-delta": + if event.delta.message.content.text: + print(event.delta.message.content.text, end="", flush=True) ``` ```