Skip to content

Commit 3d1a7da

Browse files
fix(retrieve): enhance document ID extraction to support S3 URIs (#249)
1 parent bc1aaff commit 3d1a7da

File tree

5 files changed

+103
-80
lines changed

5 files changed

+103
-80
lines changed

src/strands_tools/code_interpreter/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@
1717
1818
Example:
1919
>>> from strands_tools.code_interpreter import AgentCoreCodeInterpreter
20-
>>>
20+
>>>
2121
>>> # Default usage
2222
>>> interpreter = AgentCoreCodeInterpreter(region="us-west-2")
23-
>>>
23+
>>>
2424
>>> # Custom identifier usage
2525
>>> custom_interpreter = AgentCoreCodeInterpreter(
2626
... region="us-west-2",

src/strands_tools/code_interpreter/agent_core_code_interpreter.py

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@
2424
class SessionInfo:
2525
"""
2626
Information about a code interpreter session.
27-
27+
2828
This dataclass stores the essential information for managing active code
2929
interpreter sessions, including the session identifier, description, and
3030
the underlying Bedrock client instance.
31-
31+
3232
Attributes:
3333
session_id (str): Unique identifier for the session assigned by AWS Bedrock.
3434
description (str): Human-readable description of the session purpose.
@@ -44,43 +44,43 @@ class SessionInfo:
4444
class AgentCoreCodeInterpreter(CodeInterpreter):
4545
"""
4646
Bedrock AgentCore implementation of the CodeInterpreter.
47-
47+
4848
This class provides a code interpreter interface using AWS Bedrock AgentCore services.
4949
It supports executing Python, JavaScript, and TypeScript code in isolated sandbox
5050
environments with custom code interpreter identifiers.
51-
51+
5252
The class maintains session state and provides methods for code execution, file
5353
operations, and session management. It supports both default AWS code interpreter
5454
environments and custom environments specified by identifier.
55-
55+
5656
Examples:
5757
Basic usage with default identifier:
58-
58+
5959
>>> interpreter = AgentCoreCodeInterpreter(region="us-west-2")
6060
>>> # Uses default identifier: "aws.codeinterpreter.v1"
61-
61+
6262
Using a custom code interpreter identifier:
63-
63+
6464
>>> custom_id = "my-custom-interpreter-abc123"
6565
>>> interpreter = AgentCoreCodeInterpreter(
66-
... region="us-west-2",
66+
... region="us-west-2",
6767
... identifier=custom_id
6868
... )
69-
69+
7070
Environment-specific usage:
71-
71+
7272
>>> # For testing environments
7373
>>> test_interpreter = AgentCoreCodeInterpreter(
7474
... region="us-east-1",
7575
... identifier="test-interpreter-xyz789"
7676
... )
77-
78-
>>> # For production environments
77+
78+
>>> # For production environments
7979
>>> prod_interpreter = AgentCoreCodeInterpreter(
8080
... region="us-west-2",
8181
... identifier="prod-interpreter-def456"
8282
... )
83-
83+
8484
Attributes:
8585
region (str): The AWS region where the code interpreter service is hosted.
8686
identifier (str): The code interpreter identifier being used for sessions.
@@ -97,24 +97,24 @@ def __init__(self, region: Optional[str] = None, identifier: Optional[str] = Non
9797
identifier (Optional[str]): Custom code interpreter identifier to use
9898
for code execution sessions. This allows you to specify custom code
9999
interpreter environments instead of the default AWS-provided one.
100-
100+
101101
Valid formats include:
102102
- Default identifier: "aws.codeinterpreter.v1" (used when None)
103103
- Custom identifier: "my-custom-interpreter-abc123"
104104
- Environment-specific: "test-interpreter-xyz789"
105-
105+
106106
Note: Use the code interpreter ID, not the full ARN. The AWS service
107107
expects the identifier portion only (e.g., "my-interpreter-123" rather
108108
than "arn:aws:bedrock-agentcore:region:account:code-interpreter-custom/my-interpreter-123").
109-
109+
110110
If not provided, defaults to "aws.codeinterpreter.v1" for backward
111111
compatibility. Defaults to None.
112-
112+
113113
Note:
114114
This constructor maintains full backward compatibility. Existing code
115115
that doesn't specify the identifier parameter will continue to work
116116
unchanged with the default AWS code interpreter environment.
117-
117+
118118
Raises:
119119
Exception: If there are issues with AWS region resolution or client
120120
initialization during session creation.
@@ -152,20 +152,20 @@ def cleanup_platform(self) -> None:
152152
def init_session(self, action: InitSessionAction) -> Dict[str, Any]:
153153
"""
154154
Initialize a new Bedrock AgentCore sandbox session.
155-
155+
156156
Creates a new code interpreter session using the configured identifier.
157157
The session will use the identifier specified during class initialization,
158158
or the default "aws.codeinterpreter.v1" if none was provided.
159-
159+
160160
Args:
161161
action (InitSessionAction): Action containing session initialization parameters
162162
including session_name and description.
163-
163+
164164
Returns:
165165
Dict[str, Any]: Response dictionary containing session information on success
166166
or error details on failure. Success response includes sessionName,
167167
description, and sessionId.
168-
168+
169169
Raises:
170170
Exception: If session initialization fails due to AWS service issues,
171171
invalid identifier, or other configuration problems.
@@ -219,8 +219,8 @@ def init_session(self, action: InitSessionAction) -> Dict[str, Any]:
219219
f"Failed to initialize session '{session_name}' with identifier: {self.identifier}. Error: {str(e)}"
220220
)
221221
return {
222-
"status": "error",
223-
"content": [{"text": f"Failed to initialize session '{session_name}': {str(e)}"}]
222+
"status": "error",
223+
"content": [{"text": f"Failed to initialize session '{session_name}': {str(e)}"}],
224224
}
225225

226226
def list_local_sessions(self) -> Dict[str, Any]:

src/strands_tools/retrieve.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -187,14 +187,21 @@ def format_results_for_display(results: List[Dict[str, Any]]) -> str:
187187
results: List of retrieval results from Bedrock Knowledge Base
188188
189189
Returns:
190-
Formatted string containing the results in a readable format
190+
Formatted string containing the results in a readable format, including score, document ID, and content.
191191
"""
192192
if not results:
193193
return "No results found above score threshold."
194194

195195
formatted = []
196196
for result in results:
197-
doc_id = result.get("location", {}).get("customDocumentLocation", {}).get("id", "Unknown")
197+
# Extract document location - handle both s3Location and customDocumentLocation
198+
location = result.get("location", {})
199+
doc_id = "Unknown"
200+
if "customDocumentLocation" in location:
201+
doc_id = location["customDocumentLocation"].get("id", "Unknown")
202+
elif "s3Location" in location:
203+
# Extract meaningful part from S3 URI
204+
doc_id = location["s3Location"].get("uri", "")
198205
score = result.get("score", 0.0)
199206
formatted.append(f"\nScore: {score:.4f}")
200207
formatted.append(f"Document ID: {doc_id}")

0 commit comments

Comments
 (0)