Skip to content

Commit 10797fd

Browse files
committed
feat(Chat): The .append_message() method now automatically streams in generators
1 parent 7ae7725 commit 10797fd

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

shiny/ui/_chat.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -515,13 +515,21 @@ async def append_message(self, message: Any) -> None:
515515
The message to append. A variety of message formats are supported including
516516
a string, a dictionary with `content` and `role` keys, or a relevant chat
517517
completion object from platforms like OpenAI, Anthropic, Ollama, and others.
518+
When the message is a generator or async generator, it is automatically
519+
treated as a stream of message chunks (i.e., uses
520+
`.append_message_stream()`)
518521
519522
Note
520523
----
521-
Use `.append_message_stream()` instead of this method when `stream=True` (or
522-
similar) is specified in model's completion method.
524+
Although this method tries its best to handle various message formats, it's
525+
not always possible to handle every message format. If you encounter an error
526+
or no response when appending a message, try extracting the message content
527+
as a string and passing it to this method.
523528
"""
524-
await self._append_message(message)
529+
if inspect.isasyncgen(message) or inspect.isgenerator(message):
530+
await self.append_message_stream(message)
531+
else:
532+
await self._append_message(message)
525533

526534
async def _append_message(
527535
self, message: Any, *, chunk: ChunkOption = False, stream_id: str | None = None

0 commit comments

Comments
 (0)