Repository navigation
fix(bedrock): record the system prompt and tools for Anthropic invoke_model - #4014
Conversation
mikeldking
left a comment
There was a problem hiding this comment.
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_paramsWith 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
|
Thanks, applied both in 0c9f003: |
ialisaleh
left a comment
There was a problem hiding this comment.
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!
…_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.
d0d393e to
2e63810
Compare
…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.
Maintainer's noteI pushed these changes to this branch.
|
Fixes #4010.
Problem
For an Anthropic Messages body sent through Bedrock, the two
invoke_modelpaths recorded different request data:invoke_model(utils/anthropic/_attributes.py):get_llm_input_attributesbuiltllm.input_messagesonly frommessages. The top-levelsystemprompt was never recorded as a system message, andtoolsnever reachedllm.tools.invoke_model_with_response_stream(utils/anthropic/_messages.py): for asystemgiven as a list of text blocks,_attributes_from_system_messagebuilt the content key without{prefix}, so the text landed undermessage.contents.N.message_content.textinstead ofllm.input_messages.0.message.contents.N.message_content.text.Fix
llm.input_messages.0with rolesystem, and pass the tool definitions toget_llm_attributes(tools=...).json_schemais the full tool definition, the same value the streaming path already records.{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 prompttest_streaming_request_records_system_text_blocks_under_input_messagesWith the source change reverted, all three fail (
assert 'user' == 'system'and aKeyErroron the prefixed key). With it,tox run -e py314-ci-bedrockis green: ruff format/check, mypy, 154 passed.Out of scope
The issue also notes the two paths differ on
llm.model_name(responsemodelvs requestmodelId) and that the streaming span has nollm.provider/llm.system. Those change existing span values, so I left them for a follow-up. #3950 touches the same two files (droppingsystemfrom invocation parameters); the changes are complementary.🤖 Generated with Claude Code