Skip to content

Commit 3524742

Browse files
committed
Fix typing issues
1 parent d0d7719 commit 3524742

File tree

3 files changed

+10
-6
lines changed

3 files changed

+10
-6
lines changed

shiny/ui/_chat.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -489,7 +489,7 @@ def messages(
489489
transform_user == "last" and i == len(messages) - 1
490490
)
491491
content_key = m["transform_key" if transform else "pre_transform_key"]
492-
chat_msg = ChatMessage(content=m[content_key], role=m["role"])
492+
chat_msg = ChatMessage(content=str(m[content_key]), role=m["role"])
493493
if not isinstance(format, MISSING_TYPE):
494494
chat_msg = as_provider_message(chat_msg, format)
495495
res.append(chat_msg)
@@ -790,7 +790,7 @@ async def _transform_message(
790790
if content is None:
791791
return None
792792

793-
res[key] = content
793+
res[key] = content # type: ignore
794794

795795
return res
796796

@@ -950,7 +950,7 @@ def user_input(self, transform: bool = False) -> str | None:
950950
if msg is None:
951951
return None
952952
key = "content_server" if transform else "content_client"
953-
return msg[key]
953+
return str(msg[key])
954954

955955
def _user_input(self) -> str:
956956
id = self.user_input_id

shiny/ui/_chat_types.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
from typing import Literal, TypedDict
44

5+
from htmltools import HTML
6+
57
Role = Literal["assistant", "user", "system"]
68

79

@@ -14,14 +16,16 @@ class ChatMessage(TypedDict):
1416

1517
# A message once transformed have been applied
1618
class TransformedMessage(TypedDict):
17-
content_client: str
19+
content_client: str | HTML
1820
content_server: str
1921
role: Role
2022
transform_key: Literal["content_client", "content_server"]
2123
pre_transform_key: Literal["content_client", "content_server"]
2224

2325

2426
# A message that can be sent to the client
25-
class ClientMessage(ChatMessage):
27+
class ClientMessage(TypedDict):
28+
content: str | HTML
29+
role: Role
2630
content_type: Literal["markdown", "html"]
2731
chunk_type: Literal["message_start", "message_end"] | None

tests/playwright/shiny/components/chat/transform_assistant/app.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
# TODO: test with append_message_stream() as well
1414
@chat.transform_assistant_response
15-
def transform(content: str) -> str:
15+
def transform(content: str) -> str | ui.HTML:
1616
if content == "return HTML":
1717
return ui.HTML(f"<b>Transformed response</b>: {content}")
1818
else:

0 commit comments

Comments
 (0)