|
13 | 13 | from typing import TYPE_CHECKING |
14 | 14 |
|
15 | 15 | 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 |
18 | 17 |
|
19 | 18 | from sentry_sdk.tracing import Span |
20 | 19 |
|
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 | | - |
70 | 20 |
|
71 | 21 | def set_request_span_data(span, kwargs, integration, config, span_data=None): |
72 | 22 | # type: (Span, Dict[str, Any], Any, Dict[str, Any], Dict[str, Any] | None) -> None |
|
0 commit comments