Jump to content

Connect Leeroopedia MCP: Equip your AI agents to search best practices, build plans, verify code, diagnose failures, and look up hyperparameter defaults.

Implementation:Cohere ai Cohere python Map Response From Bedrock

From Leeroopedia
Property Value
Type Implementation
Domain Response Transformation, AWS Bedrock, Adapter Pattern
Source Cohere Python SDK
Last Updated 2026-02-15
Implements Principle:Cohere_ai_Cohere_python_Bedrock_Response_Transformation

Overview

Concrete response transformation functions for converting Bedrock API responses to standard Cohere types.

Description

map_response_from_bedrock() returns an httpx event hook that processes responses. For non-streaming: parses JSON body, maps to Cohere response types using response_mapping dict, injects token counts from headers. For streaming (application/vnd.amazon.eventstream): stream_generator decodes base64 event stream chunks into Cohere streaming response types. map_token_counts extracts usage from Bedrock headers.

Code Reference

  • src/cohere/aws_client.py Lines L138-197 (stream_generator, map_token_counts, map_response_from_bedrock)
  • src/cohere/aws_client.py Lines L125-136 (response_mapping)

Signature

def map_response_from_bedrock() -> EventHook:
def stream_generator(response: httpx.Response, endpoint: str) -> Iterator[bytes]:
def map_token_counts(response: httpx.Response) -> ApiMeta:

Import

Internal; used automatically by BedrockClientV2.

Inputs

  • Raw httpx.Response from Bedrock endpoint

Outputs

  • Transformed response matching standard Cohere SDK types (V2ChatResponse, EmbedResponse, RerankResponse)

Example

# Transparent usage — users don't interact with transformation directly
bedrock = BedrockClientV2(aws_region="us-east-1")
response = bedrock.chat(model="cohere.command-r-plus-v1:0", messages=messages)
# response is a standard V2ChatResponse, transformation happened internally
print(response.usage)  # Token counts extracted from Bedrock headers

Related

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment