-
Notifications
You must be signed in to change notification settings - Fork 1k
fix(botocore): tag Bedrock embedding InvokeModel calls as embeddings #4997
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
Open
BetterAndBetterII
wants to merge
6
commits into
open-telemetry:main
Choose a base branch
from
BetterAndBetterII:fix/bedrock-embed-op-name
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
3e1477e
fix(botocore): tag Bedrock embedding InvokeModel calls as embeddings
BetterAndBetterII 67a35ba
Add changelog fragment for Bedrock embedding operation name fix
BetterAndBetterII e6f20be
fix(botocore): match embedding modelId on last dotted segment
BetterAndBetterII 6e10970
Merge branch 'main' into fix/bedrock-embed-op-name
BetterAndBetterII bb779e0
Merge branch 'main' into fix/bedrock-embed-op-name
BetterAndBetterII 4ac87d3
Merge branch 'main' into fix/bedrock-embed-op-name
BetterAndBetterII 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| `opentelemetry-instrumentation-botocore`: tag Bedrock embedding InvokeModel calls as embeddings |
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
58 changes: 58 additions & 0 deletions
58
...ation-botocore/tests/cassettes/test_invoke_model_with_embeddings_model[amazon.titan].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,58 @@ | ||
| interactions: | ||
| - request: | ||
| body: |- | ||
| { | ||
| "inputText": "Say this is a test" | ||
| } | ||
| headers: | ||
| Content-Length: | ||
| - '36' | ||
| User-Agent: | ||
| - Boto3/1.35.56 md/Botocore#1.35.56 ua/2.0 os/macos#24.0.0 md/arch#arm64 lang/python#3.13.1 | ||
| md/pyimpl#CPython cfg/retry-mode#legacy Botocore/1.35.56 | ||
| X-Amz-Date: | ||
| - 20250826T103000Z | ||
| X-Amzn-Trace-Id: | ||
| - Root=1-aaaaaaaa-bbbbbbbbbbbbbbbbbbbbbbbb;Parent=cccccccccccccccc;Sampled=1 | ||
| amz-sdk-invocation-id: | ||
| - 11111111-2222-3333-4444-555555555555 | ||
| amz-sdk-request: | ||
| - attempt=1 | ||
| authorization: | ||
| - Bearer test_aws_authorization | ||
| method: POST | ||
| uri: https://bedrock-runtime.us-east-1.amazonaws.com/model/amazon.titan-embed-text-v1/invoke | ||
| response: | ||
| body: | ||
| string: |- | ||
| { | ||
| "embedding": [ | ||
| 0.0135, | ||
| -0.0272, | ||
| 0.0047, | ||
| 0.0189, | ||
| -0.0091, | ||
| 0.0023, | ||
| 0.0314, | ||
| -0.0168 | ||
| ], | ||
| "inputTextTokenCount": 5 | ||
| } | ||
| headers: | ||
| Connection: | ||
| - keep-alive | ||
| Content-Type: | ||
| - application/json | ||
| Date: | ||
| - Wed, 26 Aug 2026 10:30:00 GMT | ||
| Set-Cookie: test_set_cookie | ||
| X-Amzn-Bedrock-Input-Token-Count: | ||
| - '5' | ||
| X-Amzn-Bedrock-Invocation-Latency: | ||
| - '120' | ||
| x-amzn-RequestId: | ||
| - aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee | ||
| status: | ||
| code: 200 | ||
| message: OK | ||
| version: 1 |
67 changes: 67 additions & 0 deletions
67
...ation-botocore/tests/cassettes/test_invoke_model_with_embeddings_model[cohere.embed].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,67 @@ | ||
| interactions: | ||
| - request: | ||
| body: |- | ||
| { | ||
| "texts": [ | ||
| "Say this is a test" | ||
| ], | ||
| "input_type": "search_document" | ||
| } | ||
| headers: | ||
| Content-Length: | ||
| - '76' | ||
| User-Agent: | ||
| - Boto3/1.35.56 md/Botocore#1.35.56 ua/2.0 os/macos#24.0.0 md/arch#arm64 lang/python#3.13.1 | ||
| md/pyimpl#CPython cfg/retry-mode#legacy Botocore/1.35.56 | ||
| X-Amz-Date: | ||
| - 20250826T103001Z | ||
| X-Amzn-Trace-Id: | ||
| - Root=1-ffffffff-eeeeeeeeeeeeeeeeeeeeeeee;Parent=dddddddddddddddd;Sampled=1 | ||
| amz-sdk-invocation-id: | ||
| - 66666666-7777-8888-9999-000000000000 | ||
| amz-sdk-request: | ||
| - attempt=1 | ||
| authorization: | ||
| - Bearer test_aws_authorization | ||
| method: POST | ||
| uri: https://bedrock-runtime.us-east-1.amazonaws.com/model/cohere.embed-v4%3A0/invoke | ||
| response: | ||
| body: | ||
| string: |- | ||
| { | ||
| "id": "836a33cc-61ec-4e65-afaf-c4628171a315", | ||
| "embeddings": [ | ||
| [ | ||
| 0.0135, | ||
| -0.0272, | ||
| 0.0047, | ||
| 0.0189, | ||
| -0.0091, | ||
| 0.0023, | ||
| 0.0314, | ||
| -0.0168 | ||
| ] | ||
| ], | ||
| "response_type": "embeddings_floats", | ||
| "texts": [ | ||
| "Say this is a test" | ||
| ] | ||
| } | ||
| headers: | ||
| Connection: | ||
| - keep-alive | ||
| Content-Type: | ||
| - application/json | ||
| Date: | ||
| - Wed, 26 Aug 2026 10:30:01 GMT | ||
| Set-Cookie: test_set_cookie | ||
| X-Amzn-Bedrock-Input-Token-Count: | ||
| - '5' | ||
| X-Amzn-Bedrock-Invocation-Latency: | ||
| - '95' | ||
| x-amzn-RequestId: | ||
| - 836a33cc-61ec-4e65-afaf-c4628171a315 | ||
| 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 |
|---|---|---|
|
|
@@ -1269,6 +1269,68 @@ def get_model_name_from_family(llm_model): | |
| return llm_model_name[llm_model] | ||
|
|
||
|
|
||
| def get_embeddings_model_name_from_family(model_family): | ||
| return { | ||
| "amazon.titan": "amazon.titan-embed-text-v1", | ||
| "cohere.embed": "cohere.embed-v4:0", | ||
| }[model_family] | ||
|
|
||
|
|
||
| def get_invoke_embeddings_body(llm_model): | ||
| if "cohere.embed" in llm_model: | ||
| return json.dumps( | ||
| { | ||
| "texts": ["Say this is a test"], | ||
| "input_type": "search_document", | ||
| } | ||
| ) | ||
| if "amazon.titan" in llm_model: | ||
| return json.dumps({"inputText": "Say this is a test"}) | ||
| raise ValueError(f"No embeddings config for {llm_model}") | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "model_family", | ||
| [ | ||
| "amazon.titan", | ||
| "cohere.embed", | ||
| ], | ||
| ) | ||
| @pytest.mark.vcr() | ||
| def test_invoke_model_with_embeddings_model( | ||
| span_exporter, | ||
| log_exporter, | ||
| bedrock_runtime_client, | ||
| instrument_with_content, | ||
| model_family, | ||
| ): | ||
| llm_model_value = get_embeddings_model_name_from_family(model_family) | ||
| body = get_invoke_embeddings_body(llm_model_value) | ||
| response = bedrock_runtime_client.invoke_model( | ||
| body=body, | ||
| modelId=llm_model_value, | ||
| ) | ||
|
|
||
| response_body = json.loads(response["body"].read()) | ||
| assert response_body | ||
| if model_family == "amazon.titan": | ||
| assert "embedding" in response_body | ||
| elif model_family == "cohere.embed": | ||
| assert "embeddings" in response_body | ||
| else: | ||
| pytest.xfail(f"model family not handled: {model_family}") | ||
|
|
||
| (span,) = span_exporter.get_finished_spans() | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The new test also pins this: |
||
| assert_stream_completion_attributes( | ||
| span, | ||
| llm_model_value, | ||
| operation_name="embeddings", | ||
| ) | ||
|
|
||
| logs = log_exporter.get_finished_logs() | ||
| assert len(logs) == 0 | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "model_family", | ||
| [ | ||
|
|
||
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.
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.
IIUC this makes
gen_ai.usage.input_tokensdisappear for Titan embed models on main the response reaches_handle_amazon_titan_response, which sets it frominputTextTokenCount.