Skip to content

Commit 3e787de

Browse files
committed
chore(python-sdk): modernise type hints
Leverage __future__ import annotations to enable support for streamlined type hints using | vs Union from PEP 604. Future annotations was enabled across the codebase so that any additional use of Optional or Union would be flagged by type linters. Removed unused imports, guarded import under TYPE_CHECKING where necessary. Fixed accept parameter definition for EventEncoder and added a missing comment. Fixes: #50
1 parent 32a74f0 commit 3e787de

File tree

32 files changed

+228
-179
lines changed

32 files changed

+228
-179
lines changed

docs/sdk/python/core/events.mdx

Lines changed: 35 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,15 @@ shared across all event types.
4848
```python
4949
class BaseEvent(ConfiguredBaseModel):
5050
type: EventType
51-
timestamp: Optional[int] = None
52-
raw_event: Optional[Any] = None
51+
timestamp: int | None = None
52+
raw_event: Any = None
5353
```
5454

5555
| Property | Type | Description |
5656
| ----------- | --------------- | ----------------------------------------------------- |
5757
| `type` | `EventType` | The type of event (discriminator field for the union) |
58-
| `timestamp` | `Optional[int]` | Timestamp when the event was created |
59-
| `raw_event` | `Optional[Any]` | Original event data if this event was transformed |
58+
| `timestamp` | `int | None` | Timestamp when the event was created |
59+
| `raw_event` | `Any` | Original event data if this event was transformed |
6060

6161
## Lifecycle Events
6262

@@ -91,14 +91,14 @@ class RunFinishedEvent(BaseEvent):
9191
type: Literal[EventType.RUN_FINISHED]
9292
thread_id: str
9393
run_id: str
94-
result: Optional[Any] = None
94+
result: Any = None
9595
```
9696

9797
| Property | Type | Description |
9898
| ----------- | --------------- | ------------------------------ |
9999
| `thread_id` | `str` | ID of the conversation thread |
100100
| `run_id` | `str` | ID of the agent run |
101-
| `result` | `Optional[Any]` | Result data from the agent run |
101+
| `result` | `Any` | Result data from the agent run |
102102

103103
### RunErrorEvent
104104

@@ -110,13 +110,13 @@ Signals an error during an agent run.
110110
class RunErrorEvent(BaseEvent):
111111
type: Literal[EventType.RUN_ERROR]
112112
message: str
113-
code: Optional[str] = None
113+
code: str | None = None
114114
```
115115

116116
| Property | Type | Description |
117117
| --------- | --------------- | ------------- |
118118
| `message` | `str` | Error message |
119-
| `code` | `Optional[str]` | Error code |
119+
| `code` | `str | None` | Error code |
120120

121121
### StepStartedEvent
122122

@@ -225,14 +225,14 @@ class ToolCallStartEvent(BaseEvent):
225225
type: Literal[EventType.TOOL_CALL_START]
226226
tool_call_id: str
227227
tool_call_name: str
228-
parent_message_id: Optional[str] = None
228+
parent_message_id: str | None = None
229229
```
230230

231231
| Property | Type | Description |
232232
| ------------------- | --------------- | ----------------------------------- |
233233
| `tool_call_id` | `str` | Unique identifier for the tool call |
234234
| `tool_call_name` | `str` | Name of the tool being called |
235-
| `parent_message_id` | `Optional[str]` | ID of the parent message |
235+
| `parent_message_id` | `str | None` | ID of the parent message |
236236

237237
### ToolCallArgsEvent
238238

@@ -280,15 +280,15 @@ class ToolCallResultEvent(BaseEvent):
280280
type: Literal[EventType.TOOL_CALL_RESULT]
281281
tool_call_id: str
282282
content: str
283-
role: Optional[Literal["tool"]] = None
283+
role: Literal["tool"] | None = None
284284
```
285285

286286
| Property | Type | Description |
287287
| -------------- | --------------------------- | ----------------------------------------------------------- |
288288
| `message_id` | `str` | ID of the conversation message this result belongs to |
289289
| `tool_call_id` | `str` | Matches the ID from the corresponding ToolCallStartEvent |
290290
| `content` | `str` | The actual result/output content from the tool execution |
291-
| `role` | `Optional[Literal["tool"]]` | Optional role identifier, typically "tool" for tool results |
291+
| `role` | `Literal["tool"] | None` | Optional role identifier, typically "tool" for tool results |
292292

293293
## State Management Events
294294

@@ -354,13 +354,13 @@ Used to pass through events from external systems.
354354
class RawEvent(BaseEvent):
355355
type: Literal[EventType.RAW]
356356
event: Any
357-
source: Optional[str] = None
357+
source: str | None = None
358358
```
359359

360360
| Property | Type | Description |
361361
| -------- | --------------- | ------------------- |
362362
| `event` | `Any` | Original event data |
363-
| `source` | `Optional[str]` | Source of the event |
363+
| `source` | `str | None` | Source of the event |
364364

365365
### CustomEvent
366366

@@ -388,25 +388,27 @@ The SDK uses Pydantic's discriminated unions for event validation:
388388

389389
```python
390390
Event = Annotated[
391-
Union[
392-
TextMessageStartEvent,
393-
TextMessageContentEvent,
394-
TextMessageEndEvent,
395-
ToolCallStartEvent,
396-
ToolCallArgsEvent,
397-
ToolCallEndEvent,
398-
ToolCallResultEvent,
399-
StateSnapshotEvent,
400-
StateDeltaEvent,
401-
MessagesSnapshotEvent,
402-
RawEvent,
403-
CustomEvent,
404-
RunStartedEvent,
405-
RunFinishedEvent,
406-
RunErrorEvent,
407-
StepStartedEvent,
408-
StepFinishedEvent,
409-
],
391+
TextMessageStartEvent |
392+
TextMessageContentEvent |
393+
TextMessageEndEvent |
394+
TextMessageChunkEvent |
395+
ToolCallStartEvent |
396+
ToolCallArgsEvent |
397+
ToolCallEndEvent |
398+
ToolCallChunkEvent |
399+
ToolCallResultEvent |
400+
ThinkingStartEvent |
401+
ThinkingEndEvent |
402+
StateSnapshotEvent |
403+
StateDeltaEvent |
404+
MessagesSnapshotEvent |
405+
RawEvent |
406+
CustomEvent |
407+
RunStartedEvent |
408+
RunFinishedEvent |
409+
RunErrorEvent |
410+
StepStartedEvent |
411+
StepFinishedEvent,
410412
Field(discriminator="type")
411413
]
412414
```

docs/sdk/python/core/types.mdx

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ class DeveloperMessage(BaseMessage):
7171
| `id` | `str` | Unique identifier for the message |
7272
| `role` | `Literal["developer"]` | Role of the message sender, fixed as "developer" |
7373
| `content` | `str` | Text content of the message (required) |
74-
| `name` | `Optional[str]` | Optional name of the sender |
74+
| `name` | `str | None` | Optional name of the sender |
7575

7676
### SystemMessage
7777

@@ -90,7 +90,7 @@ class SystemMessage(BaseMessage):
9090
| `id` | `str` | Unique identifier for the message |
9191
| `role` | `Literal["system"]` | Role of the message sender, fixed as "system" |
9292
| `content` | `str` | Text content of the message (required) |
93-
| `name` | `Optional[str]` | Optional name of the sender |
93+
| `name` | `str | None` | Optional name of the sender |
9494

9595
### AssistantMessage
9696

@@ -101,17 +101,17 @@ Represents a message from an assistant.
101101
```python
102102
class AssistantMessage(BaseMessage):
103103
role: Literal["assistant"]
104-
content: Optional[str] = None
105-
tool_calls: Optional[List[ToolCall]] = None
104+
content: str | None = None
105+
tool_calls: List[ToolCall] | None = None
106106
```
107107

108108
| Property | Type | Description |
109109
| ------------ | -------------------------- | ------------------------------------------------ |
110110
| `id` | `str` | Unique identifier for the message |
111111
| `role` | `Literal["assistant"]` | Role of the message sender, fixed as "assistant" |
112-
| `content` | `Optional[str]` | Text content of the message |
113-
| `name` | `Optional[str]` | Name of the sender |
114-
| `tool_calls` | `Optional[List[ToolCall]]` | Tool calls made in this message |
112+
| `content` | `str | None`. | Text content of the message |
113+
| `name` | `str | None` | Name of the sender |
114+
| `tool_calls` | `List[ToolCall] | None` | Tool calls made in this message |
115115

116116
### UserMessage
117117

@@ -130,7 +130,7 @@ class UserMessage(BaseMessage):
130130
| `id` | `str` | Unique identifier for the message |
131131
| `role` | `Literal["user"]` | Role of the message sender, fixed as "user" |
132132
| `content` | `str` | Text content of the message (required) |
133-
| `name` | `Optional[str]` | Optional name of the sender |
133+
| `name` | `str | None` | Optional name of the sender |
134134

135135
### ToolMessage
136136

@@ -161,7 +161,7 @@ A union type representing any type of message in the system.
161161

162162
```python
163163
Message = Annotated[
164-
Union[DeveloperMessage, SystemMessage, AssistantMessage, UserMessage, ToolMessage],
164+
DeveloperMessage | SystemMessage | AssistantMessage | UserMessage | ToolMessage,
165165
Field(discriminator="role")
166166
]
167167
```

python-sdk/ag_ui/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
"""
2+
Agent User Interaction Protocol Python SDK
3+
"""

python-sdk/ag_ui/core/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
This module contains the core types and events for the Agent User Interaction Protocol.
33
"""
44

5+
from __future__ import annotations
6+
57
from ag_ui.core.events import (
68
EventType,
79
BaseEvent,

0 commit comments

Comments
 (0)