Debian Codegraph @

src/anthropic/lib/streaming/_types.py

raw ◀ ▶
1 Debian patch touch this file
1 from typing import TYPE_CHECKING, Any, Dict, Union, Generic, cast
2 from typing_extensions import List, Literal, Annotated
3
•4 try:
5 from jiter import from_json
6 except ImportError:
7 from pydantic_core import from_json
8
9 from ...types import (
10 Message,
11 ContentBlock,
12 MessageDeltaEvent as RawMessageDeltaEvent,
13 MessageStartEvent as RawMessageStartEvent,
14 RawMessageStopEvent,
15 ContentBlockDeltaEvent as RawContentBlockDeltaEvent,
16 ContentBlockStartEvent as RawContentBlockStartEvent,
17 RawContentBlockStopEvent,
18 )
19 from ..._models import BaseModel, GenericModel, UnionDiscriminator
20 from .._parse._response import ResponseFormatT
21 from ...types.parsed_message import ParsedMessage, ParsedContentBlock
22 from ...types.citations_delta import Citation
23
24
25 class TextEvent(BaseModel):
26 type: Literal["text"]
27
28 text: str
29 """The text delta"""
30
31 snapshot: str
32 """The entire accumulated text"""
33
34 def parsed_snapshot(self) -> Dict[str, Any]:
35 return cast(Dict[str, Any], jiter.from_json(self.snapshot.encode("utf-8"), partial_mode="trailing-strings"))
36
37
38 class CitationEvent(BaseModel):
39 type: Literal["citation"]
40
41 citation: Citation
42 """The new citation"""
43
44 snapshot: List[Citation]
45 """All of the accumulated citations"""
46
47
48 class ThinkingEvent(BaseModel):
49 type: Literal["thinking"]
50
51 thinking: str
52 """The thinking delta"""
53
54 snapshot: str
55 """The accumulated thinking so far"""
56
57
58 class SignatureEvent(BaseModel):
59 type: Literal["signature"]
60
61 signature: str
62 """The signature of the thinking block"""
63
64
65 class InputJsonEvent(BaseModel):
66 type: Literal["input_json"]
67
68 partial_json: str
69 """A partial JSON string delta
70
71 e.g. `'"San Francisco,'`
72 """
73
74 snapshot: object
75 """The currently accumulated parsed object.
76
77
78 e.g. `{'location': 'San Francisco, CA'}`
79 """
80
81
82 class MessageStopEvent(RawMessageStopEvent):
83 type: Literal["message_stop"]
84
85 message: Message
86
87
88 class ContentBlockStopEvent(RawContentBlockStopEvent):
89 type: Literal["content_block_stop"]
90
91 content_block: ContentBlock
92
93
94 MessageStreamEvent = Annotated[
95 Union[
96 TextEvent,
97 CitationEvent,
98 ThinkingEvent,
99 SignatureEvent,
100 InputJsonEvent,
101 RawMessageStartEvent,
102 RawMessageDeltaEvent,
103 MessageStopEvent,
104 RawContentBlockStartEvent,
105 RawContentBlockDeltaEvent,
106 ContentBlockStopEvent,
107 ],
108 UnionDiscriminator("type"),
109 ]
110
111
112 class ParsedMessageStopEvent(RawMessageStopEvent, GenericModel, Generic[ResponseFormatT]):
113 type: Literal["message_stop"]
114
115 if TYPE_CHECKING:
116 message: ParsedMessage[ResponseFormatT]
117 else:
118 message: ParsedMessage
119
120
121 class ParsedContentBlockStopEvent(RawContentBlockStopEvent, GenericModel, Generic[ResponseFormatT]):
122 type: Literal["content_block_stop"]
123
124 if TYPE_CHECKING:
125 content_block: ParsedContentBlock[ResponseFormatT]
126 else:
127 content_block: ParsedContentBlock
128
129
130 ParsedMessageStreamEvent = Annotated[
131 Union[
132 TextEvent,
133 CitationEvent,
134 ThinkingEvent,
135 SignatureEvent,
136 InputJsonEvent,
137 RawMessageStartEvent,
138 RawMessageDeltaEvent,
139 ParsedMessageStopEvent[ResponseFormatT],
140 RawContentBlockStartEvent,
141 RawContentBlockDeltaEvent,
142 ParsedContentBlockStopEvent[ResponseFormatT],
143 ],
144 UnionDiscriminator("type"),
145 ]