-
Notifications
You must be signed in to change notification settings - Fork 10
feat(integrations): aws boto3 sdk #521
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
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
py/src/braintrust/integrations/auto_test_scripts/test_auto_boto3_sdk.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| import os | ||
|
|
||
| import boto3 | ||
| from braintrust.auto import auto_instrument | ||
| from braintrust.integrations.boto3.patchers import Boto3ConversePatcher | ||
| from braintrust.integrations.test_utils import autoinstrument_test_context | ||
|
|
||
|
|
||
| def is_patched(target, patcher): | ||
| return bool(getattr(target, patcher.nested_patch_marker_attr(), False)) | ||
|
|
||
|
|
||
| # verify not patched initially | ||
| original_client = boto3.client( | ||
| "bedrock-runtime", | ||
| aws_access_key_id="test-key", | ||
| aws_secret_access_key="test-access-key", | ||
| aws_session_token="test-session-key", | ||
| region_name="us-east-1", | ||
| ) | ||
|
|
||
| assert is_patched(original_client, Boto3ConversePatcher) is False | ||
|
|
||
| # instrument | ||
| results = auto_instrument() | ||
| assert results.get("boto3") is True | ||
|
|
||
| # patch | ||
| patched_client = boto3.client( | ||
| "bedrock-runtime", | ||
| aws_access_key_id="", | ||
| aws_secret_access_key="", | ||
| aws_session_token="", | ||
| region_name="us-east-1", | ||
| ) | ||
|
|
||
| # idempotent | ||
| results = auto_instrument() | ||
| assert results.get("boto3") is True | ||
|
|
||
|
|
||
| assert is_patched(patched_client, Boto3ConversePatcher) is True | ||
|
|
||
| # make api call | ||
| with autoinstrument_test_context("test_auto_boto3", integration="boto3") as memory_logger: | ||
| client = boto3.client( | ||
| "bedrock-runtime", | ||
| region_name="us-east-1", | ||
| aws_access_key_id=os.getenv("AWS_ACCESS_KEY_ID"), | ||
| aws_secret_access_key=os.getenv("AWS_SECRET_ACCESS_KEY"), | ||
| aws_session_token=os.getenv("AWS_SESSION_TOKEN"), | ||
| ) | ||
|
|
||
| response = response = client.converse( | ||
| modelId="anthropic.claude-3-haiku-20240307-v1:0", | ||
| messages=[{"role": "user", "content": [{"text": "what's 2+2"}]}], | ||
| system=[{"text": "answer only in single integer"}], | ||
| ) | ||
|
|
||
| spans = memory_logger.pop() | ||
| assert len(spans) == 1, f"Expected 1 span, got {len(spans)}" | ||
|
|
||
| span = spans[0] | ||
| assert span["metadata"]["model_provider"] == "anthropic" | ||
| assert "claude" in span["metadata"]["model"] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| import logging | ||
|
|
||
| from braintrust.logger import NOOP_SPAN, current_span, init_logger | ||
|
|
||
| from .integration import Boto3Integration | ||
|
|
||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
| __all__ = ["Boto3Integration", "setup_boto3"] | ||
|
|
||
|
|
||
| def setup_boto3( | ||
| api_key: str | None = None, | ||
| project_id: str | None = None, | ||
| project_name: str | None = None, | ||
| ) -> bool: | ||
| span = current_span() | ||
|
|
||
| if span == NOOP_SPAN: | ||
| init_logger(project=project_name, api_key=api_key, project_id=project_id) | ||
|
|
||
| return Boto3Integration.setup() |
62 changes: 62 additions & 0 deletions
62
py/src/braintrust/integrations/boto3/cassettes/latest/test_auto_boto3.yaml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| interactions: | ||
| - request: | ||
| body: '{"messages": [{"role": "user", "content": [{"text": "what''s 2+2"}]}], | ||
| "system": [{"text": "answer only in single integer"}]}' | ||
| headers: | ||
| Content-Length: | ||
| - '124' | ||
| Content-Type: | ||
| - !!binary | | ||
| YXBwbGljYXRpb24vanNvbg== | ||
| User-Agent: | ||
| - !!binary | | ||
| Qm90bzMvMS40My4yNiBtZC9Cb3RvY29yZSMxLjQzLjI4IHVhLzIuMSBvcy9saW51eCM2LjguMC0x | ||
| MDUyLWF6dXJlIG1kL2FyY2gjeDg2XzY0IGxhbmcvcHl0aG9uIzMuMTIuMSBtZC9weWltcGwjQ1B5 | ||
| dGhvbiBtL1osYixELGUgY2ZnL3JldHJ5LW1vZGUjbGVnYWN5IEJvdG9jb3JlLzEuNDMuMjg= | ||
| X-Amz-Date: | ||
| - !!binary | | ||
| MjAyNjA2MTJUMDkwMjE5Wg== | ||
| X-Amz-Security-Token: | ||
| - !!binary | | ||
| SVFvSmIzSnBaMmx1WDJWakVFWWFDWFZ6TFhkbGMzUXRNaUpITUVVQ0lFSlVDZDFxQlZncWhMd3BM | ||
| cF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UytGRGRnYldPZXVjZEV6TWluWnNNRXJ4RUExK1dZMVNtMkp6alN6VmFOc0NZTDZXQ3A5dlNneUE3 | ||
| U001aDFQVjlLRCtnPQ== | ||
| amz-sdk-invocation-id: | ||
| - !!binary | | ||
| MzkwYzg5YjktMjM4Ni00NWI1LTkzZTYtYjgyMjc1YWM2OWY2 | ||
| amz-sdk-request: | ||
| - !!binary | | ||
| YXR0ZW1wdD0x | ||
| method: POST | ||
| uri: https://bedrock-runtime.us-east-1.amazonaws.com/model/anthropic.claude-3-haiku-20240307-v1%3A0/converse | ||
| response: | ||
| body: | ||
| string: '{"metrics":{"latencyMs":269},"output":{"message":{"content":[{"text":"4"}],"role":"assistant"}},"stopReason":"end_turn","usage":{"inputTokens":18,"outputTokens":5,"serverToolUsage":{},"totalTokens":23}}' | ||
| headers: | ||
| Connection: | ||
| - keep-alive | ||
| Content-Length: | ||
| - '202' | ||
| Content-Type: | ||
| - application/json | ||
| Date: | ||
| - Fri, 12 Jun 2026 09:02:20 GMT | ||
| x-amzn-RequestId: | ||
| - 20c1e6b5-f545-444a-8de6-a47e4c4c295a | ||
| status: | ||
| code: 200 | ||
| message: OK | ||
| version: 1 |
62 changes: 62 additions & 0 deletions
62
py/src/braintrust/integrations/boto3/cassettes/latest/test_boto3_converse.yaml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| interactions: | ||
| - request: | ||
| body: '{"messages": [{"role": "user", "content": [{"text": "what''s 2+2"}]}], | ||
| "system": [{"text": "answer only in single integer"}]}' | ||
| headers: | ||
| Content-Length: | ||
| - '124' | ||
| Content-Type: | ||
| - !!binary | | ||
| YXBwbGljYXRpb24vanNvbg== | ||
| User-Agent: | ||
| - !!binary | | ||
| Qm90bzMvMS40My4yNiBtZC9Cb3RvY29yZSMxLjQzLjI4IHV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yTVg1ZlhtWTlJMVBlZlV1M21KQTRFNlhN | ||
| aHNncmlYaWQ5THY1UFJoMU1ybTUvN2pOZTVkLzhuSTdJYTZlLytwTVJXYXEreHVvYzIxQWdob2JF | ||
| Q3lWUU1XcWZKSWZrclcxMGpMU3FPYlMwd2tMbXUwUVk2bVFHdmZCaitadTN6TnNpL0hRRUIzRjB6 | ||
| QWtmRENHYmhYbXJrTzVjRkJPNG0zdFdjNmJoWGlST2o0QVRmVVRzSGVvOFlzOExhTEdsSGtSbmdt | ||
| WWc4Y3dPVG5FSTBUNldUQjVEYm41L2IybUN6dHdJNHdCTFBUUUhkcVRxVDJ2M05JbC8zTitpd0dQ | ||
| UytGRGRnYldPZXVjZEV6TWluWnNNRXJ4RUExK1dZMVNtMkp6alN6VmFOc0NZTDZXQ3A5dlNneUE3 | ||
| U001aDFQVjlLRCtnPQ== | ||
| amz-sdk-invocation-id: | ||
| - !!binary | | ||
| MzBlZjQ1MzktMmFlNS00M2IwLTkwMjUtMWQwZDU3MDkzMjQ5 | ||
| amz-sdk-request: | ||
| - !!binary | | ||
| YXR0ZW1wdD0x | ||
| method: POST | ||
| uri: https://bedrock-runtime.us-east-1.amazonaws.com/model/anthropic.claude-3-haiku-20240307-v1%3A0/converse | ||
| response: | ||
| body: | ||
| string: '{"metrics":{"latencyMs":257},"output":{"message":{"content":[{"text":"4"}],"role":"assistant"}},"stopReason":"end_turn","usage":{"inputTokens":18,"outputTokens":5,"serverToolUsage":{},"totalTokens":23}}' | ||
| headers: | ||
| Connection: | ||
| - keep-alive | ||
| Content-Length: | ||
| - '202' | ||
| Content-Type: | ||
| - application/json | ||
| Date: | ||
| - Fri, 12 Jun 2026 06:13:02 GMT | ||
| x-amzn-RequestId: | ||
| - f9de30ea-4863-4786-9de1-a01f0232e54c | ||
| status: | ||
| code: 200 | ||
| message: OK | ||
| version: 1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| from braintrust.integrations.base import BaseIntegration | ||
|
|
||
| from .patchers import Boto3ConversePatcher | ||
|
|
||
|
|
||
| class Boto3Integration(BaseIntegration): | ||
| name = "boto3_integration" | ||
| patchers = (Boto3ConversePatcher,) | ||
| import_names = ("botocore",) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| from braintrust.integrations.base import NestedFunctionWrapperPatcher | ||
|
|
||
| from .tracing import converse_tracer | ||
|
|
||
|
|
||
| class Boto3ConversePatcher(NestedFunctionWrapperPatcher): | ||
| """Patcher class for Boto3.BedrockRuntime.Client.Converse API""" | ||
|
|
||
| name = "boto3" | ||
| target_module = "botocore.client" | ||
| target_path = "ClientCreator._create_client_class" | ||
| target_attribute = "converse" | ||
| nested_wrapper = converse_tracer |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
could have followed the similar approach to Anthropics patching, I found this simple, convenient, it's ugly but this works.