Skip to content

Commit aa1dbfa

Browse files
committed
move type definition
1 parent 1e975ce commit aa1dbfa

2 files changed

Lines changed: 52 additions & 53 deletions

File tree

sentry_sdk/ai/span_config.py

Lines changed: 1 addition & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -13,60 +13,10 @@
1313
from typing import TYPE_CHECKING
1414

1515
if TYPE_CHECKING:
16-
from typing import Any, Callable, Dict, List, Optional, Sequence, Tuple
17-
from typing_extensions import TypedDict
16+
from typing import Any, Dict, List, Optional
1817

1918
from sentry_sdk.tracing import Span
2019

21-
# Source paths: list of attribute chains to try in order.
22-
# e.g. [("meta", "billed_units", "input_tokens"), ("meta", "tokens", "input_tokens")]
23-
SourcePaths = Sequence[Tuple[str, ...]]
24-
25-
# Maps a SPANDATA key to source paths on the response object.
26-
# e.g. {SPANDATA.GEN_AI_RESPONSE_ID: [("id",)]}
27-
SourceMapping = Dict[str, SourcePaths]
28-
29-
class UsageConfig(TypedDict, total=False):
30-
"""Declarative token usage extraction paths (from response object)."""
31-
32-
input_tokens: SourcePaths
33-
output_tokens: SourcePaths
34-
total_tokens: SourcePaths
35-
36-
class ResponseConfig(TypedDict, total=False):
37-
"""Declarative response span data config."""
38-
39-
# Attributes always extracted from the response object.
40-
sources: SourceMapping
41-
# Attributes extracted only when PII sending is enabled.
42-
pii_sources: SourceMapping
43-
# Custom extractor for response text (PII only).
44-
# Returns list of text strings, or None.
45-
extract_text: Callable[[Any], Optional[List[str]]]
46-
# Declarative token usage paths.
47-
usage: UsageConfig
48-
49-
class OperationConfig(TypedDict, total=False):
50-
"""Full declarative config for an AI operation (chat, embeddings, etc.)."""
51-
52-
# Key/value pairs set on every span unconditionally.
53-
static: Dict[str, Any]
54-
# Maps kwarg names to SPANDATA keys (always set if present in kwargs).
55-
params: Dict[str, str]
56-
# Maps kwarg names to SPANDATA keys (only set when PII is enabled).
57-
pii_params: Dict[str, str]
58-
# Extracts messages from kwargs for the span.
59-
extract_messages: Callable[[Dict[str, Any]], Optional[List[Dict[str, Any]]]]
60-
# SPANDATA key for messages (default: GEN_AI_REQUEST_MESSAGES).
61-
message_target: str
62-
# Non-streaming response config.
63-
response: ResponseConfig
64-
# Streaming response config (different attribute paths).
65-
stream_response: ResponseConfig
66-
# Source paths to extract a full response object from a stream-end event
67-
# (V1 pattern: reuse "response" config after extracting).
68-
stream_response_object: SourcePaths
69-
7020

7121
def set_request_span_data(span, kwargs, integration, config, span_data=None):
7222
# type: (Span, Dict[str, Any], Any, Dict[str, Any], Dict[str, Any] | None) -> None

sentry_sdk/integrations/cohere/configs.py

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,57 @@
77
from typing import TYPE_CHECKING
88

99
if TYPE_CHECKING:
10-
from typing import Any
11-
from sentry_sdk.ai.span_config import OperationConfig
10+
from typing import Any, Callable, Dict, List, Optional, Sequence, Tuple
11+
from typing_extensions import TypedDict
12+
13+
# Source paths: list of attribute chains to try in order.
14+
# e.g. [("meta", "billed_units", "input_tokens"), ("meta", "tokens", "input_tokens")]
15+
SourcePaths = Sequence[Tuple[str, ...]]
16+
17+
# Maps a SPANDATA key to source paths on the response object.
18+
# e.g. {SPANDATA.GEN_AI_RESPONSE_ID: [("id",)]}
19+
SourceMapping = Dict[str, SourcePaths]
20+
21+
class UsageConfig(TypedDict, total=False):
22+
"""Declarative token usage extraction paths (from response object)."""
23+
24+
input_tokens: SourcePaths
25+
output_tokens: SourcePaths
26+
total_tokens: SourcePaths
27+
28+
class ResponseConfig(TypedDict, total=False):
29+
"""Declarative response span data config."""
30+
31+
# Attributes always extracted from the response object.
32+
sources: SourceMapping
33+
# Attributes extracted only when PII sending is enabled.
34+
pii_sources: SourceMapping
35+
# Custom extractor for response text (PII only).
36+
# Returns list of text strings, or None.
37+
extract_text: Callable[[Any], Optional[List[str]]]
38+
# Declarative token usage paths.
39+
usage: UsageConfig
40+
41+
class OperationConfig(TypedDict, total=False):
42+
"""Full declarative config for an AI operation (chat, embeddings, etc.)."""
43+
44+
# Key/value pairs set on every span unconditionally.
45+
static: Dict[str, Any]
46+
# Maps kwarg names to SPANDATA keys (always set if present in kwargs).
47+
params: Dict[str, str]
48+
# Maps kwarg names to SPANDATA keys (only set when PII is enabled).
49+
pii_params: Dict[str, str]
50+
# Extracts messages from kwargs for the span.
51+
extract_messages: Callable[[Dict[str, Any]], Optional[List[Dict[str, Any]]]]
52+
# SPANDATA key for messages (default: GEN_AI_REQUEST_MESSAGES).
53+
message_target: str
54+
# Non-streaming response config.
55+
response: ResponseConfig
56+
# Streaming response config (different attribute paths).
57+
stream_response: ResponseConfig
58+
# Source paths to extract a full response object from a stream-end event
59+
# (V1 pattern: reuse "response" config after extracting).
60+
stream_response_object: SourcePaths
1261

1362

1463
# ── Helpers ──────────────────────────────────────────────────────────────────

0 commit comments

Comments
 (0)