Skip to content

feat: support redactedContent in reasoningContent streamed events #134

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/strands/event_loop/streaming.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,18 @@ def handle_content_block_delta(
**kwargs,
)

elif "redactedContent" in delta_content["reasoningContent"]:
if "redactedContent" not in state:
state["redactedContent"] = b""

state["redactedContent"] += delta_content["reasoningContent"]["redactedContent"]
callback_handler(
redactedContent=delta_content["reasoningContent"]["redactedContent"],
delta=delta_content,
reasoning=True,
**kwargs,
)

return state


Expand Down Expand Up @@ -207,6 +219,9 @@ def handle_content_block_stop(state: Dict[str, Any]) -> Dict[str, Any]:
}
)
state["reasoningText"] = ""
elif "redactedContent" in state and state["redactedContent"]:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: add a line like redacted_content = state["redactedContent"] on line 185 above for consistent style with the other state variables used in these conditions

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

content.append({"reasoningContent": {"redactedContent": state["redactedContent"]}})
state["redactedContent"] = b""

return state

Expand Down Expand Up @@ -279,6 +294,7 @@ def process_stream(
"current_tool_use": {},
"reasoningText": "",
"signature": "",
"redactedContent": b"",
}
state["content"] = state["message"]["content"]

Expand Down
61 changes: 61 additions & 0 deletions tests/strands/event_loop/test_streaming.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,20 @@ def test_handle_content_block_start(chunk: ContentBlockStartEvent, exp_tool_use)
{"signature": "val"},
{"reasoning_signature": "val", "reasoning": True},
),
# Reasoning - redactedContent - New
(
{"delta": {"reasoningContent": {"redactedContent": b"encrypted"}}},
{},
{"redactedContent": b"encrypted"},
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

super nit: encoded might be better terminology than encrypted. The value is a base64 encoded binary blob

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right. Fixed it.

{"redactedContent": b"encrypted", "reasoning": True},
),
# Reasoning - redactedContent - Existing
(
{"delta": {"reasoningContent": {"redactedContent": b"data"}}},
{"redactedContent": b"encrypted_"},
{"redactedContent": b"encrypted_data"},
{"redactedContent": b"data", "reasoning": True},
),
# Reasoning - Empty
(
{"delta": {"reasoningContent": {}}},
Expand Down Expand Up @@ -230,6 +244,23 @@ def callback_handler(**kwargs):
"signature": "123",
},
),
# redactedContent
(
{
"content": [],
"current_tool_use": {},
"text": "",
"reasoningText": "",
"redactedContent": b"encrypted_data",
},
{
"content": [{"reasoningContent": {"redactedContent": b"encrypted_data"}}],
"current_tool_use": {},
"text": "",
"reasoningText": "",
"redactedContent": b"",
},
),
# Empty
(
{
Expand Down Expand Up @@ -355,6 +386,36 @@ def test_extract_usage_metrics():
{"calls": 1},
[{"role": "user", "content": [{"text": "REDACTED"}]}],
),
(
[
{"messageStart": {"role": "assistant"}},
{
"contentBlockStart": {"start": {}},
},
{
"contentBlockDelta": {"delta": {"reasoningContent": {"redactedContent": b"encrypted_data"}}},
},
{"contentBlockStop": {}},
{
"messageStop": {"stopReason": "end_turn"},
},
{
"metadata": {
"usage": {"inputTokens": 1, "outputTokens": 1, "totalTokens": 1},
"metrics": {"latencyMs": 1},
}
},
],
"end_turn",
{
"role": "assistant",
"content": [{"reasoningContent": {"redactedContent": b"encrypted_data"}}],
},
{"inputTokens": 1, "outputTokens": 1, "totalTokens": 1},
{"latencyMs": 1},
{"calls": 1},
[{"role": "user", "content": [{"text": "Some input!"}]}],
),
],
)
def test_process_stream(
Expand Down
Loading