Skip to content

bedrock_utils.py throwing an error on #3958

@brnaba-aws

Description

@brnaba-aws

Describe your environment

OS: Bedrock agentcore runtime
Python version: 3.13
Package version: 0.12.2

What happened?

It appears that in bedrock_utils.py at this line
the response doesn't contain any output and because of that, it is throwing an error:

See this log:

"exception.stacktrace": "Traceback (most recent call last):
  File \"/usr/local/lib/python3.13/site-packages/strands/event_loop/event_loop.py\", line 189, in event_loop_cycle
    async for tool_event in tool_events:
        yield tool_event
  File \"/usr/local/lib/python3.13/site-packages/strands/event_loop/event_loop.py\", line 531, in _handle_tool_execution
    async for event in events:
        yield event
  File \"/usr/local/lib/python3.13/site-packages/strands/event_loop/event_loop.py\", line 277, in recurse_event_loop
    async for event in events:
        yield event
  File \"/usr/local/lib/python3.13/site-packages/strands/event_loop/event_loop.py\", line 152, in event_loop_cycle
    async for model_event in model_events:
        if not isinstance(model_event, ModelStopReason):
            yield model_event
  File \"/usr/local/lib/python3.13/site-packages/strands/event_loop/event_loop.py\", line 396, in _handle_model_execution
    raise e
  File \"/usr/local/lib/python3.13/site-packages/strands/event_loop/event_loop.py\", line 337, in _handle_model_execution
    async for event in stream_messages(
    ...<7 lines>...
        yield event
  File \"/usr/local/lib/python3.13/site-packages/strands/event_loop/streaming.py\", line 457, in stream_messages
    async for event in process_stream(chunks, start_time):
        yield event
  File \"/usr/local/lib/python3.13/site-packages/strands/event_loop/streaming.py\", line 391, in process_stream
    async for chunk in chunks:
    ...<22 lines>...
            handle_redact_content(chunk[\"redactContent\"], state)
  File \"/usr/local/lib/python3.13/site-packages/strands/models/bedrock.py\", line 647, in stream
    await task
  File \"/usr/local/lib/python3.13/asyncio/threads.py\", line 25, in to_thread
    return await loop.run_in_executor(None, func_call)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File \"/usr/local/lib/python3.13/concurrent/futures/thread.py\", line 59, in run
    result = self.fn(*self.args, **self.kwargs)
  File \"/usr/local/lib/python3.13/site-packages/opentelemetry/instrumentation/threading/__init__.py\", line 171, in wrapped_func
    return original_func(*func_args, **func_kwargs)
  File \"/usr/local/lib/python3.13/site-packages/strands/models/bedrock.py\", line 686, in _stream
    for chunk in response[\"stream\"]:
                 ~~~~~~~~^^^^^^^^^^
  File \"/usr/local/lib/python3.13/site-packages/opentelemetry/instrumentation/botocore/extensions/bedrock_utils.py\", line 72, in __iter__
    self._process_event(event)
    ~~~~~~~~~~~~~~~~~~~^^^^^^^
  File \"/usr/local/lib/python3.13/site-packages/amazon/opentelemetry/distro/patches/_botocore_patches.py\", line 295, in patched_process_event
    old_process_event(self, event)
    ~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^
  File \"/usr/local/lib/python3.13/site-packages/opentelemetry/instrumentation/botocore/extensions/bedrock_utils.py\", line 137, in _process_event
    self._stream_done_callback(self._response)
    ~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^
  File \"/usr/local/lib/python3.13/site-packages/opentelemetry/instrumentation/botocore/extensions/bedrock.py\", line 642, in stream_done_callback
    self._converse_on_success(
    ~~~~~~~~~~~~~~~~~~~~~~~~~^
        span, response, instrumentor_context, capture_content
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    )
    ^
  File \"/usr/local/lib/python3.13/site-packages/opentelemetry/instrumentation/botocore/extensions/bedrock.py\", line 503, in _converse_on_success
    choice = _Choice.from_converse(result, capture_content)
  File \"/usr/local/lib/python3.13/site-packages/opentelemetry/instrumentation/botocore/extensions/bedrock_utils.py\", line 493, in from_converse
    orig_message = response[\"output\"][\"message\"]
                   ~~~~~~~~^^^^^^^^^^
KeyError: 'output'
",

No checks are being made on the response content. Maybe good idea to have some sort of .get()??

Steps to Reproduce

Hard to do

Expected Result

shouldn't throw an error

Actual Result

the code crashes

Additional context

No response

Would you like to implement a fix?

None

Tip

React with 👍 to help prioritize this issue. Please use comments to provide useful context, avoiding +1 or me too, to help us triage it. Learn more here.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions