Skip to content

fix(bedrock): record the system prompt and tools for Anthropic invoke_model - #4014

Merged
caroger merged 6 commits into
Arize-ai:mainfrom
pcbeingused333:fix-bedrock-invoke-model-system-tools
Oct 20, 2026
Merged

caroger merged 6 commits into
Arize-ai:mainfrom
pcbeingused333:fix-bedrock-invoke-model-system-tools

Conversation

@pcbeingused333

@pcbeingused333 pcbeingused333 commented Oct 10, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #4010.

Problem

For an Anthropic Messages body sent through Bedrock, the two invoke_model paths recorded different request data:

  1. Non-streaming invoke_model (utils/anthropic/_attributes.py): get_llm_input_attributes built llm.input_messages only from messages. The top-level system prompt was never recorded as a system message, and tools never reached llm.tools.
  2. Streaming invoke_model_with_response_stream (utils/anthropic/_messages.py): for a system given as a list of text blocks, _attributes_from_system_message built the content key without {prefix}, so the text landed under message.contents.N.message_content.text instead of llm.input_messages.0.message.contents.N.message_content.text.

Fix

  • Non-streaming: prepend the system prompt (string or text blocks) as llm.input_messages.0 with role system, and pass the tool definitions to get_llm_attributes(tools=...). json_schema is the full tool definition, the same value the streaming path already records.
  • Streaming: add the missing {prefix}.

Tests

Added to test_invoke_model_claude_message_api.py:

  • test_invoke_model_records_system_prompt_and_tools, parametrized over a string and a text-block system prompt
  • test_streaming_request_records_system_text_blocks_under_input_messages

With the source change reverted, all three fail (assert 'user' == 'system' and a KeyError on the prefixed key). With it, tox run -e py314-ci-bedrock is green: ruff format/check, mypy, 154 passed.

Out of scope

The issue also notes the two paths differ on llm.model_name (response model vs request modelId) and that the streaming span has no llm.provider / llm.system. Those change existing span values, so I left them for a follow-up. #3950 touches the same two files (dropping system from invocation parameters); the changes are complementary.

🤖 Generated with Claude Code

@mikeldking mikeldking left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks. Recording system and tools on the non-streaming path, and the missing {prefix} fix, both look right.

One masking gap: _validate_invocation_parameter in utils/anthropic/_attributes.py (not in this diff) still leaves system and tools in llm.invocation_parameters. hide_inputs, hide_input_messages and hide_input_text don't mask that attribute. I ran a stubbed invoke_model with TraceConfig(hide_inputs=True): llm.input_messages.* and input.value were masked, but llm.invocation_parameters still contained "system": [{"text": "SECRET-SYS"}]. The streaming path and the Anthropic instrumentor already drop these keys.

--- a/python/instrumentation/openinference-instrumentation-bedrock/src/openinference/instrumentation/bedrock/utils/anthropic/_attributes.py
+++ b/python/instrumentation/openinference-instrumentation-bedrock/src/openinference/instrumentation/bedrock/utils/anthropic/_attributes.py
@@ -257,6 +257,9 @@ def _validate_invocation_parameter(parameter: Any) -> bool:
     excluded_params = (
         "messages",
         "modelId",
+        # recorded as llm.input_messages / llm.tools, where TraceConfig masking applies
+        "system",
+        "tools",
     )
     return parameter not in excluded_params

With this change and the test assertion suggested inline, tox -e py314-ci-bedrock passes: 154 tests, plus ruff and mypy.

Heads-up: #3950 makes the same {prefix} change and adds "system" to the same tuple, so whichever PR lands second will have a small conflict there.


Generated by Claude Code

@pcbeingused333

Copy link
Copy Markdown
Contributor Author

Thanks, applied both in 0c9f003: system and tools are excluded from llm.invocation_parameters, and the test now asserts the invocation parameters are only anthropic_version and max_tokens. That assertion fails with the exclusion reverted. Bedrock suite: 154 passed, ruff and mypy clean. Noted the overlap with #3950 and will resolve whichever conflict lands on this side.

@ialisaleh ialisaleh left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I reviewed this PR and confirmed that it records system prompts and tool definitions for Anthropic Claude Messages requests in both streaming and nonstreaming calls. I added the reported llm.provider and llm.system attributes, and made completed streaming spans use the response model name, as nonstreaming spans do. I also excluded anthropic_version from nonstreaming invocation parameters to match streaming. I added coverage for multiple system blocks and tools, absent or empty system prompts, and completed or empty streams. The package tests and code quality checks pass, so this looks ready to merge. Thanks!

pcbeingused333 and others added 5 commits October 19, 2026 15:10
…_model

The non-streaming invoke_model path built llm.input_messages only from
the request's `messages`, so the top-level `system` prompt was never
recorded as a system message and the `tools` definitions never reached
llm.tools. The streaming path recorded both; the non-streaming path now
does too, with the same json_schema (the full tool definition).

The streaming path had its own bug: for a system prompt given as a list
of text blocks, the content key was built without the message prefix, so
the text landed under `message.contents.N.message_content.text` instead
of `llm.input_messages.0.message.contents.N.message_content.text`.

Fixes Arize-ai#4010.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
They are now recorded as llm.input_messages and llm.tools, where
TraceConfig masking applies; leaving them in llm.invocation_parameters
exposed the system prompt with hide_inputs enabled.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…fied request attributes

After rebasing on Arize-ai#3950, both invoke paths drop anthropic_version from
llm.invocation_parameters, set llm.provider and llm.system, and record a
string system prompt as a text content block.
@caroger
caroger force-pushed the fix-bedrock-invoke-model-system-tools branch from d0d393e to 2e63810 Compare October 19, 2026 22:21
…very stream attribute

test_request_attributes_agree_across_paths already checks string and block
system prompts, tools, and invocation parameters on both paths.
test_stream_final_model_name now pops every attribute and asserts none are left.
@caroger
caroger merged commit d7dbb9c into Arize-ai:main Oct 20, 2026
25 checks passed
@mikeldking mikeldking mentioned this pull request Oct 19, 2026
@caroger

caroger commented Oct 20, 2026

Copy link
Copy Markdown
Contributor

Maintainer's note

I pushed these changes to this branch.

  • Rebased onto main (force-push from d0d393e0). fix(bedrock): redact Anthropic system prompt fields #3950 merged changes to the same Anthropic request-attribute code, so this PR conflicted with main. The conflict was resolved by keeping fix(bedrock): redact Anthropic system prompt fields #3950's system-prompt message shape and adding this PR's llm.tools and invocation-parameter changes on top.
  • 2e63810a test(bedrock): align fix(bedrock): redact Anthropic system prompt fields #3950's system-prompt tests with the unified request attributes. After the rebase, both request paths drop anthropic_version from llm.invocation_parameters, set llm.provider and llm.system, and record a string system prompt as a text content block. fix(bedrock): redact Anthropic system prompt fields #3950's tests and this PR's string-prompt test expected the older values and failed, so they now assert the new ones.
  • 854aa91a test(bedrock): drop tests the cross-path parity test covers; assert every stream attribute. test_request_attributes_agree_across_paths already covers what test_invoke_model_records_system_prompt_and_tools and test_streaming_request_records_system_text_blocks_under_input_messages checked, so those two were removed. test_stream_final_model_name now asserts every attribute on the span, so an unexpected attribute fails it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

[bug] bedrock: invoke_model drops the system prompt and tools; streaming writes system blocks under a malformed key

4 participants