Skip to content

Commit f492e53

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 af51738 commit f492e53

File tree

8 files changed

+61
-41
lines changed

8 files changed

+61
-41
lines changed

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,

python-sdk/ag_ui/core/events.py

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22
This module contains the event types for the Agent User Interaction Protocol Python SDK.
33
"""
44

5+
from __future__ import annotations
6+
57
from enum import Enum
6-
from typing import Any, List, Literal, Optional, Union, Annotated
8+
from typing import Any, List, Literal, Optional, Annotated
79
from pydantic import Field
810

911
from .types import Message, State, ConfiguredBaseModel
@@ -38,8 +40,8 @@ class BaseEvent(ConfiguredBaseModel):
3840
Base event for all events in the Agent User Interaction Protocol.
3941
"""
4042
type: EventType
41-
timestamp: Optional[int] = None
42-
raw_event: Optional[Any] = None
43+
timestamp: int | None = None
44+
raw_event: Any = None
4345

4446

4547
class TextMessageStartEvent(BaseEvent):
@@ -76,9 +78,9 @@ class TextMessageChunkEvent(BaseEvent):
7678
Event containing a chunk of text message content.
7779
"""
7880
type: Literal[EventType.TEXT_MESSAGE_CHUNK]
79-
message_id: Optional[str] = None
81+
message_id: str | None = None
8082
role: Optional[Literal["assistant"]] = None
81-
delta: Optional[str] = None
83+
delta: str | None = None
8284

8385
class ToolCallStartEvent(BaseEvent):
8486
"""
@@ -87,7 +89,7 @@ class ToolCallStartEvent(BaseEvent):
8789
type: Literal[EventType.TOOL_CALL_START]
8890
tool_call_id: str
8991
tool_call_name: str
90-
parent_message_id: Optional[str] = None
92+
parent_message_id: str | None = None
9193

9294

9395
class ToolCallArgsEvent(BaseEvent):
@@ -111,10 +113,10 @@ class ToolCallChunkEvent(BaseEvent):
111113
Event containing a chunk of tool call content.
112114
"""
113115
type: Literal[EventType.TOOL_CALL_CHUNK]
114-
tool_call_id: Optional[str] = None
115-
tool_call_name: Optional[str] = None
116-
parent_message_id: Optional[str] = None
117-
delta: Optional[str] = None
116+
tool_call_id: str | None = None
117+
tool_call_name: str | None = None
118+
parent_message_id: str | None = None
119+
delta: str | None = None
118120

119121
class StateSnapshotEvent(BaseEvent):
120122
"""
@@ -146,7 +148,7 @@ class RawEvent(BaseEvent):
146148
"""
147149
type: Literal[EventType.RAW]
148150
event: Any
149-
source: Optional[str] = None
151+
source: str | None = None
150152

151153

152154
class CustomEvent(BaseEvent):
@@ -182,7 +184,7 @@ class RunErrorEvent(BaseEvent):
182184
"""
183185
type: Literal[EventType.RUN_ERROR]
184186
message: str
185-
code: Optional[str] = None
187+
code: str | None = None
186188

187189

188190
class StepStartedEvent(BaseEvent):
@@ -202,25 +204,23 @@ class StepFinishedEvent(BaseEvent):
202204

203205

204206
Event = Annotated[
205-
Union[
206-
TextMessageStartEvent,
207-
TextMessageContentEvent,
208-
TextMessageEndEvent,
209-
TextMessageChunkEvent,
210-
ToolCallStartEvent,
211-
ToolCallArgsEvent,
212-
ToolCallEndEvent,
213-
ToolCallChunkEvent,
214-
StateSnapshotEvent,
215-
StateDeltaEvent,
216-
MessagesSnapshotEvent,
217-
RawEvent,
218-
CustomEvent,
219-
RunStartedEvent,
220-
RunFinishedEvent,
221-
RunErrorEvent,
222-
StepStartedEvent,
223-
StepFinishedEvent,
224-
],
207+
TextMessageStartEvent |
208+
TextMessageContentEvent |
209+
TextMessageEndEvent |
210+
TextMessageChunkEvent |
211+
ToolCallStartEvent |
212+
ToolCallArgsEvent |
213+
ToolCallEndEvent |
214+
ToolCallChunkEvent |
215+
StateSnapshotEvent |
216+
StateDeltaEvent |
217+
MessagesSnapshotEvent |
218+
RawEvent |
219+
CustomEvent |
220+
RunStartedEvent |
221+
RunFinishedEvent |
222+
RunErrorEvent |
223+
StepStartedEvent |
224+
StepFinishedEvent,
225225
Field(discriminator="type")
226226
]

python-sdk/ag_ui/core/types.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22
This module contains the types for the Agent User Interaction Protocol Python SDK.
33
"""
44

5-
from typing import Any, List, Literal, Optional, Union, Annotated
5+
from __future__ import annotations
6+
7+
from typing import Any, List, Literal, Optional, Annotated
68
from pydantic import BaseModel, Field, ConfigDict
79
from pydantic.alias_generators import to_camel
810

@@ -41,8 +43,8 @@ class BaseMessage(ConfiguredBaseModel):
4143
"""
4244
id: str
4345
role: str
44-
content: Optional[str] = None
45-
name: Optional[str] = None
46+
content: str | None = None
47+
name: str | None = None
4648

4749

4850
class DeveloperMessage(BaseMessage):
@@ -88,7 +90,7 @@ class ToolMessage(ConfiguredBaseModel):
8890

8991

9092
Message = Annotated[
91-
Union[DeveloperMessage, SystemMessage, AssistantMessage, UserMessage, ToolMessage],
93+
DeveloperMessage | SystemMessage | AssistantMessage | UserMessage | ToolMessage,
9294
Field(discriminator="role")
9395
]
9496

python-sdk/ag_ui/encoder/__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 EventEncoder class.
33
"""
44

5+
from __future__ import annotations
6+
57
from ag_ui.encoder.encoder import EventEncoder, AGUI_MEDIA_TYPE
68

79
__all__ = ["EventEncoder", "AGUI_MEDIA_TYPE"]

python-sdk/ag_ui/encoder/encoder.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,25 @@
22
This module contains the EventEncoder class
33
"""
44

5-
from ag_ui.core.events import BaseEvent
5+
from __future__ import annotations
6+
7+
from typing import TYPE_CHECKING
8+
9+
if TYPE_CHECKING:
10+
from ag_ui.core.events import BaseEvent
11+
612

713
AGUI_MEDIA_TYPE = "application/vnd.ag-ui.event+proto"
814

915
class EventEncoder:
1016
"""
1117
Encodes Agent User Interaction events.
1218
"""
13-
def __init__(self, accept: str = None):
14-
pass
19+
def __init__(self, accept: str | None = None) -> None:
20+
"""
21+
Initializes the EventEncoder.
22+
"""
23+
self.accept = accept
1524

1625
def get_content_type(self) -> str:
1726
"""

python-sdk/tests/test_encoder.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from __future__ import annotations
2+
13
import unittest
24
import json
35
from datetime import datetime

python-sdk/tests/test_events.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1+
from __future__ import annotations
2+
13
import unittest
2-
import json
34
from datetime import datetime
45
from pydantic import ValidationError, TypeAdapter
56

6-
from ag_ui.core.types import Message, UserMessage, AssistantMessage, FunctionCall, ToolCall
7+
from ag_ui.core.types import UserMessage, AssistantMessage, FunctionCall, ToolCall
78
from ag_ui.core.events import (
89
EventType,
910
BaseEvent,

python-sdk/tests/test_types.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from __future__ import annotations
2+
13
import unittest
24
from pydantic import ValidationError
35
from pydantic import TypeAdapter

0 commit comments

Comments
 (0)