Skip to content

Commit 19d711b

Browse files
authored
Anticipate htmltools.HTML no longer inheriting from str (#1690)
1 parent b45b89f commit 19d711b

File tree

5 files changed

+15
-10
lines changed

5 files changed

+15
-10
lines changed

shiny/ui/_chat.py

Lines changed: 4 additions & 4 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)
@@ -635,7 +635,7 @@ async def _send_append_message(
635635
content_type = "html" if isinstance(content, HTML) else "markdown"
636636

637637
msg = ClientMessage(
638-
content=content,
638+
content=str(content),
639639
role=message["role"],
640640
content_type=content_type,
641641
chunk_type=chunk_type,
@@ -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_normalize.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
from abc import ABC, abstractmethod
33
from typing import TYPE_CHECKING, Any, Optional, cast
44

5+
from htmltools import HTML
6+
57
from ._chat_types import ChatMessage
68

79
if TYPE_CHECKING:
@@ -49,10 +51,10 @@ def normalize_chunk(self, chunk: Any) -> ChatMessage:
4951
return ChatMessage(content=x or "", role="assistant")
5052

5153
def can_normalize(self, message: Any) -> bool:
52-
return isinstance(message, str) or message is None
54+
return isinstance(message, (str, HTML)) or message is None
5355

5456
def can_normalize_chunk(self, chunk: Any) -> bool:
55-
return isinstance(chunk, str) or chunk is None
57+
return isinstance(chunk, (str, HTML)) or chunk is None
5658

5759

5860
class DictNormalizer(BaseMessageNormalizer):

shiny/ui/_chat_types.py

Lines changed: 3 additions & 1 deletion
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,7 +16,7 @@ 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"]

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from typing import Union
2+
13
from shiny.express import render, ui
24

35
# Set some Shiny page options
@@ -12,7 +14,7 @@
1214

1315
# TODO: test with append_message_stream() as well
1416
@chat.transform_assistant_response
15-
def transform(content: str) -> str:
17+
def transform(content: str) -> Union[str, ui.HTML]:
1618
if content == "return HTML":
1719
return ui.HTML(f"<b>Transformed response</b>: {content}")
1820
else:

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
from playwright.sync_api import Page, expect
22
from utils.deploy_utils import skip_on_webkit
33

4-
from shiny import ui
54
from shiny.playwright import controller
65
from shiny.run import ShinyAppProc
76

@@ -48,7 +47,7 @@ def test_validate_chat_transform_assistant(page: Page, local_app: ShinyAppProc)
4847
{"content": "Transformed response: `hello`", "role": "assistant"},
4948
{"content": "return HTML", "role": "user"},
5049
{
51-
"content": ui.HTML("<b>Transformed response</b>: return HTML"),
50+
"content": "<b>Transformed response</b>: return HTML",
5251
"role": "assistant",
5352
},
5453
]

0 commit comments

Comments
 (0)