Bedrock Titan Model Streaming /invoke-with-response-stream


Same question as this post from 6 months ago from another user: I am making api call directly to get streaming response. how can I decode the output manually? I keep getting error

UnicodeDecodeError: 'utf-8' codec can't decode byte 0xb4 in position 9: invalid start byte

asked 2 months ago483 views
1 Answer

For Cohere this post would help:

In case of Titan I am not sure if response stream is supported. In any case I am pasting how response stream can be parsed as example, I hope this is something like this of help?

        payload = {
            "prompt": prompt,
            "max_tokens_to_sample": 1000,
            "top_k": 50,
            "temperature": 0.1

        response_stream = self.bedrock_runtime.invoke_model_with_response_stream(
            body=bytes(json.dumps(payload), "utf-8"),

        stream = response_stream.get('body')

        output = []
        if stream:
            for event in stream:
                chunk = event.get('chunk')
                if chunk:
                    chunk_obj = json.loads(chunk.get('bytes').decode())
                    text = chunk_obj['completion']

        complete_output = ''.join(output)
profile picture
answered 2 months ago
profile picture
reviewed 2 months ago
  • Thanks for the reply reply, however this is using the library. If you call the endpoint directly, the response isn’t as expected

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions