Skip to content

fix(agent): extract text from citationsContent in AgentResult.__str__#1489

Merged
zastrowm merged 1 commit intostrands-agents:mainfrom
tmokmss:fix/agent-result-citations-content
Jan 15, 2026
Merged

fix(agent): extract text from citationsContent in AgentResult.__str__#1489
zastrowm merged 1 commit intostrands-agents:mainfrom
tmokmss:fix/agent-result-citations-content

Conversation

@tmokmss
Copy link
Contributor

@tmokmss tmokmss commented Jan 15, 2026

Description

AgentResult.__str__() skips text contained in citationsContent blocks. When citations are enabled, streaming generates {"citationsContent": {"citations": [...], "content": [{"text": "..."}]}} structure, but __str__() only processes plain {"text": "..."} blocks. This causes text from cited responses to be missing when converting the result to string.

This fix adds handling for citationsContent blocks in AgentResult.__str__() to extract text from nested content.

Related Issues

closes #1488

Documentation PR

Type of Change

Bug fix

Testing

How have you tested the change? Verify that the changes do not break functionality or introduce warnings in consuming repositories: agents-docs, agents-tools, agents-cli

  • I ran hatch run prepare

Checklist

  • I have read the CONTRIBUTING document
  • I have added any necessary tests that prove my fix is effective or my feature works
  • I have updated the documentation accordingly
  • I have added an appropriate example to the documentation to outline the feature, or no new docs are needed
  • My changes generate no new warnings
  • Any dependent changes have been merged and published

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

AgentResult.__str__ now correctly extracts text from citationsContent
blocks. Previously, only plain text blocks were processed, causing
citation responses to return empty strings when converted to str().

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
@strands-agent
Copy link
Contributor

In src/strands/agent/agent_result.py at line 54, the nested check for "content" in citations_block could fail if citationsContent exists but content is missing. Consider adding a safety check:

                elif "citationsContent" in item:
                    citations_block = item.get("citationsContent", {})
                    content_list = citations_block.get("content", [])
                    for content in content_list:
                        if isinstance(content, dict) and "text" in content:
                            result += content.get("text", "") + "\n"

This uses .get() consistently to avoid KeyError if the structure is malformed.


🤖 This is an experimental AI agent response from the Strands team, powered by Strands Agents. We're exploring how AI agents can help with community support and development. Your feedback helps us improve! If you'd prefer human assistance, please let us know.

@tmokmss
Copy link
Contributor Author

tmokmss commented Jan 15, 2026

if citationsContent exists but content is missing

this never happens because we check if "content" in citations_block: before accessing citations_block["content"].

@codecov
Copy link

codecov bot commented Jan 15, 2026

Codecov Report

❌ Patch coverage is 77.77778% with 2 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
src/strands/agent/agent_result.py 77.77% 0 Missing and 2 partials ⚠️

📢 Thoughts on this report? Let us know!

@tmokmss
Copy link
Contributor Author

tmokmss commented Jan 15, 2026

While I don't think it is not really reasonable to add test cases for these branches, but let me know if you need them.

                     if "content" in citations_block: #<- if not
                         for content in citations_block["content"]:
                             if isinstance(content, dict) and "text" in content:  #<- if not
                                 result += content.get("text", "") + "\n"

@zastrowm zastrowm merged commit c23090f into strands-agents:main Jan 15, 2026
14 of 15 checks passed
@zastrowm
Copy link
Member

Thanks @tmokmss for the fix!

manoj-selvakumar5 pushed a commit to manoj-selvakumar5/strands-sdk-python that referenced this pull request Feb 5, 2026
…strands-agents#1489)

AgentResult.__str__ now correctly extracts text from citationsContent
blocks. Previously, only plain text blocks were processed, causing
citation responses to return empty strings when converted to str().

Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] AgentResult.__str__() skips text in citationsContent blocks

4 participants