Skip to content

feat(ai): Set gen_ai.tool.definitions.hash and gen_ai.system_instructions.hash - #8440

Open
sentry-junior[bot] wants to merge 5 commits into
masterfrom
feat/gen-ai-prefix-hashes
Open

sentry-junior[bot] wants to merge 5 commits into
masterfrom
feat/gen-ai-prefix-hashes

Conversation

@sentry-junior

@sentry-junior sentry-junior Bot commented Dec 2, 2026 •

Copy link
Copy Markdown
Contributor

Description

Implements the attributes proposed in getsentry/sentry-conventions#844 for the Anthropic and OpenAI (Chat Completions and Responses) integrations. This depends on that PR and shouldn't merge before it does.

The two hashes cover the stable start of a request, the tools and the system instructions. With them, a drop in gen_ai.usage.cache_read.input_tokens can be read as "the prefix changed" (a hash differs from the previous call) or "the cache expired" (both hashes and the model match).

Decisions worth a look:

  • The hashes are computed from the raw request kwargs, before normalization or truncation, and also when data_collection.gen_ai.inputs is off. That's the main reason this has to happen in the SDK. Whether that's acceptable is the open PII question on feat(attributes): Add gen_ai.tool.definitions.hash and gen_ai.system_instructions.hash sentry-conventions#844. If it isn't, the fix is to move the call under the inputs check.
  • They are skipped for NoOpSpan, so unsampled calls pay nothing. Otherwise the cost is one json.dumps and one SHA-256 of the tools and system prompt per call.
  • Only list/tuple tools and messages are hashed, so a generator passed by the caller is never consumed. Values that can't be serialized to JSON are skipped instead of guessed.
  • The hash input is json.dumps(value, separators=(",", ":"), ensure_ascii=False) of the request value, in insertion order with cache_control kept. The test values are this SDK's own vectors.

Other gen_ai integrations (Google GenAI, LiteLLM, LangChain, ...) can follow once the convention is settled.

Issues

via @BYK.

--

View Session [Sentry]

…ions.hash

Hash the tools and system instructions of Anthropic and OpenAI requests, so a reader can tell a changed prompt prefix apart from an expired provider cache. The hashes are set even when inputs are not recorded.

Co-Authored-By: BYK <126780+BYK@users.noreply.github.com>
@github-actions github-actions Bot added the risk: medium PR risk score: medium label Dec 2, 2026
@github-actions

github-actions Bot commented Dec 2, 2026 •

Copy link
Copy Markdown
Contributor

PR risk: medium

For commit 0548142.

Main concerns: reach, data sent to Sentry, host performance.

This label is a hint for reviewers, not an approval.

@github-actions

github-actions Bot commented Dec 2, 2026 •

Copy link
Copy Markdown
Contributor

Coverage Results 📊

✅ 59569 passed | ⏭️ 3149 skipped | Total: 62718 | Pass Rate: 94.98% | Execution Time: 160m 33s

📊 Comparison with Base Branch

Metric Change
Total Tests 📈 +347
Passed Tests 📈 +316
Failed Tests 📉 -1
Skipped Tests 📈 +32

➖ Removed Tests (1)

View removed tests
  • test_cron_job_ok
    • File: tests.integrations.arq.test_arq_crons

All tests are passing successfully.

✅ Patch coverage is 92.68% (38 of 41 changed executable lines covered; target 80%).
Project statement coverage is 90.18% (up 0.01 percentage points from base (d860464) to head (0548142)).

Changed files with executable lines (4)
File Patch coverage Changed executable lines
sentry_sdk/ai/utils.py 90.91% 30/33 covered; missed: 507, 508, 541; partial branches: 540, 546, 553
sentry_sdk/consts.py 100.00% 4/4 covered
sentry_sdk/integrations/anthropic.py 100.00% 1/1 covered
sentry_sdk/integrations/openai.py 100.00% 3/3 covered
Coverage diff
@@            Coverage Diff             @@
##        master     #8440       +/-##
==========================================
+ Coverage    90.17%    90.18%    +0.01%
==========================================
  Files          201       201         —
  Tracked lines     22184     22224       +40
  Branches      7772      7788       +16
==========================================
+ Hits         20003     20041       +38
- Misses        2181      2183        +2
- Partials      1283      1286        +3

Generated by Coverage Action

@github-actions github-actions Bot added risk: medium PR risk score: medium and removed risk: medium PR risk score: medium labels Dec 2, 2026
@sentry-junior
sentry-junior Bot requested a review from BYK December 2, 2026 15:49
@BYK
BYK marked this pull request as ready for review December 2, 2026 15:57
@BYK
BYK requested a review from a team as a code owner December 2, 2026 15:57
@github-actions github-actions Bot removed the risk: medium PR risk score: medium label Dec 2, 2026
…t have none

Hash list items one by one with a shared JSON encoder, and copy an item without cache_control only when its JSON has that key. The hashes stay the same.

Co-Authored-By: BYK <126780+BYK@users.noreply.github.com>
@github-actions github-actions Bot added risk: medium PR risk score: medium and removed risk: medium PR risk score: medium labels Dec 2, 2026
Hash the JSON in insertion order and keep cache_control, instead of a canonical form. A key reorder or a moved cache_control breakpoint now changes the hash, because the provider sees different bytes. The expected values are now per-SDK vectors.

Co-Authored-By: Burak Yigit Kaya <126780+BYK@users.noreply.github.com>
@github-actions github-actions Bot added risk: medium PR risk score: medium and removed risk: medium PR risk score: medium labels Dec 2, 2026

This branch has not been deployed

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

Labels

risk: medium PR risk score: medium

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant