Skip to content

[BUG]: Bedrock agent tracing bug when there's a Knowledge base trace involved #17153

@danielmurphycap

Description

@danielmurphycap

Tracer Version(s)

4.6.4

Python Version(s)

3.11

Pip Version(s)

26.0.1

Bug Report

When a bedrock trace includes a knowledge base observation type, there is an error. Replace this line in bedrock_agents.py with the code below seems to fix it:

output_value = output_chunk.get("retrievedReferences", {}).get("text", "")

retrieved = output_chunk.get("retrievedReferences", [])
if isinstance(retrieved, list):
output_value = retrieved[0].get("content", {}).get("text", "") if retrieved else ""
else:
output_value = retrieved.get("text", "")

Reproduction Code

def invoke_bedrock_agent(conversation_id: str, user_text: str) -> str:
agent_id = os.environ["BEDROCK_AGENT_ID"]
agent_alias_id = os.environ["BEDROCK_AGENT_ALIAS_ID"]

with tracer.start_as_current_span("bedrock.invoke_agent") as span:
    span.set_attribute("llm.provider", "aws_bedrock")
    span.set_attribute("bedrock.agent_id", agent_id)
    span.set_attribute("bedrock.agent_alias_id", agent_alias_id)
    span.set_attribute("bedrock.session_id", conversation_id)
    span.set_attribute("input.text", user_text[:1000])

    response = bedrock.invoke_agent(
        agentId=agent_id,
        agentAliasId=agent_alias_id,
        sessionId=conversation_id,
        inputText=user_text,
        enableTrace=True,
    )

    chunks = []
    raw_trace_events = []

    for event in response["completion"]:
        chunk = event.get("chunk")
        if chunk and chunk.get("bytes"):
            chunks.append(chunk["bytes"].decode("utf-8"))

    output_text = "".join(chunks)

    return output_text

Error Logs

Error translating Bedrock traces
Traceback (most recent call last):
File "/Users/user/Projects/cap-ai-utils/.venv/lib/python3.11/site-packages/ddtrace/contrib/internal/botocore/services/bedrock_agents.py", line 39, in iter
self._dd_integration.translate_bedrock_traces(traces, self._dd_span)
File "/Users/user/Projects/cap-ai-utils/.venv/lib/python3.11/site-packages/ddtrace/llmobs/_integrations/bedrock.py", line 176, in translate_bedrock_traces
translated_span_event, finished = translate_bedrock_trace(
^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/user/Projects/cap-ai-utils/.venv/lib/python3.11/site-packages/ddtrace/llmobs/_integrations/bedrock_agents.py", line 522, in translate_bedrock_trace
translated_span_event, finished = translation_method(trace, root_span, current_active_span_event, trace_step_id)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/user/Projects/cap-ai-utils/.venv/lib/python3.11/site-packages/ddtrace/llmobs/_integrations/bedrock_agents.py", line 225, in _translate_orchestration_trace
return _observation_span(observation, root_span, current_active_span), True
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/user/Projects/cap-ai-utils/.venv/lib/python3.11/site-packages/ddtrace/llmobs/_integrations/bedrock_agents.py", line 479, in _observation_span
output_value = output_chunk.get("retrievedReferences", {}).get("text", "")
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'list' object has no attribute 'get'
ERROR:ddtrace.contrib.internal.botocore.services.bedrock_agents:Error translating Bedrock traces
Traceback (most recent call last):
File "/Users/user/Projects/cap-ai-utils/.venv/lib/python3.11/site-packages/ddtrace/contrib/internal/botocore/services/bedrock_agents.py", line 39, in iter
self._dd_integration.translate_bedrock_traces(traces, self._dd_span)
File "/Users/user/Projects/cap-ai-utils/.venv/lib/python3.11/site-packages/ddtrace/llmobs/_integrations/bedrock.py", line 176, in translate_bedrock_traces
translated_span_event, finished = translate_bedrock_trace(
^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/user/Projects/cap-ai-utils/.venv/lib/python3.11/site-packages/ddtrace/llmobs/_integrations/bedrock_agents.py", line 522, in translate_bedrock_trace
translated_span_event, finished = translation_method(trace, root_span, current_active_span_event, trace_step_id)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/user/Projects/cap-ai-utils/.venv/lib/python3.11/site-packages/ddtrace/llmobs/_integrations/bedrock_agents.py", line 225, in _translate_orchestration_trace
return _observation_span(observation, root_span, current_active_span), True
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/user/Projects/cap-ai-utils/.venv/lib/python3.11/site-packages/ddtrace/llmobs/_integrations/bedrock_agents.py", line 479, in _observation_span
output_value = output_chunk.get("retrievedReferences", {}).get("text", "")
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'list' object has no attribute 'get'

Libraries in Use

annotated-doc==0.0.4
annotated-types==0.7.0
anyio==4.13.0
attrs==26.1.0
autopep8==2.3.2
bedrock-agentcore==1.3.0
bedrock-agentcore-starter-toolkit==0.2.10
boto3==1.42.76
botocore==1.42.76
braintrust==0.10.0
bytecode==0.17.0
certifi==2026.2.25
cffi==2.0.0
chardet==5.2.0
charset-normalizer==3.4.6
chevron==0.14.0
click==8.3.1
cryptography==46.0.5
ddtrace==4.6.4
docstring-parser==0.17.0
envier==0.6.1
exceptiongroup==1.3.1
fastapi==0.135.2
gitdb==4.0.12
gitpython==3.1.46
googleapis-common-protos==1.73.0
h11==0.16.0
httpcore==1.0.9
httpx==0.28.1
httpx-sse==0.4.3
idna==3.11
importlib-metadata==8.7.1
jinja2==3.1.6
jmespath==1.1.0
jsonschema==4.26.0
jsonschema-path==0.3.4
jsonschema-specifications==2025.9.1
lazy-object-proxy==1.12.0
markdown-it-py==4.0.0
markupsafe==3.0.3
mcp==1.26.0
mdurl==0.1.2
openapi-schema-validator==0.6.3
openapi-spec-validator==0.7.2
opentelemetry-api==1.40.0
opentelemetry-exporter-otlp-proto-common==1.40.0
opentelemetry-exporter-otlp-proto-http==1.40.0
opentelemetry-proto==1.40.0
opentelemetry-sdk==1.40.0
opentelemetry-semantic-conventions==0.61b0
packaging==26.0
pathable==0.4.4
prance==25.4.8.0
prompt-toolkit==3.0.52
protobuf==6.33.6
py-openapi-schema-to-json-schema==0.0.3
pycodestyle==2.14.0
pycparser==3.0
pydantic==2.12.5
pydantic-core==2.41.5
pydantic-settings==2.12.0
pygments==2.19.2
pyjwt==2.11.0
python-dateutil==2.9.0.post0
python-dotenv==1.2.2
python-multipart==0.0.22
python-slugify==8.0.4
pyyaml==6.0.3
questionary==2.1.1
referencing==0.37.0
requests==2.33.0
rfc3339-validator==0.1.4
rich==14.3.2
rpds-py==0.30.0
ruamel-yaml==0.19.1
s3transfer==0.16.0
shellingham==1.5.4
six==1.17.0
smmap==5.0.3
sse-starlette==3.2.0
sseclient-py==1.9.0
starlette==1.0.0
text-unidecode==1.3
toml==0.10.2
tqdm==4.67.3
typer==0.23.1
typing-extensions==4.15.0
typing-inspection==0.4.2
urllib3==2.6.3
uvicorn==0.42.0
wcwidth==0.6.0
websockets==16.0
wrapt==2.1.2
zipp==3.23.0

Operating System

Darwin Kernel Version 24.1.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions