Skip to content

Commit ebd9f44

Browse files
committed
fix(pkg-py): don't import pydantic unless it's relevant/needed
1 parent 681c154 commit ebd9f44

File tree

2 files changed

+31
-33
lines changed

2 files changed

+31
-33
lines changed

pkg-py/src/shinychat/_chat.py

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@
4646
set_chatlas_state,
4747
)
4848
from ._chat_normalize import message_content, message_content_chunk
49-
from ._chat_normalize_chatlas import hide_corresponding_request, is_tool_result
5049
from ._chat_provider_types import (
5150
AnthropicMessage, # pyright: ignore[reportAttributeAccessIssue]
5251
GoogleMessage,
@@ -69,6 +68,7 @@
6968
TransformedMessage,
7069
)
7170
from ._html_deps_py_shiny import chat_deps
71+
from ._typing_extensions import TypeGuard
7272

7373
if TYPE_CHECKING:
7474
import chatlas
@@ -1838,4 +1838,34 @@ async def append(self, message_chunk: Any):
18381838
)
18391839

18401840

1841+
async def hide_corresponding_request(x: "chatlas.ContentToolResult"):
1842+
if x.request is None:
1843+
return
1844+
1845+
session = None
1846+
try:
1847+
from shiny.session import get_current_session
1848+
1849+
session = get_current_session()
1850+
except Exception:
1851+
return
1852+
1853+
if session is None:
1854+
return
1855+
1856+
await session.send_custom_message(
1857+
"shiny-tool-request-hide",
1858+
x.request.id, # type: ignore
1859+
)
1860+
1861+
1862+
def is_tool_result(val: object) -> "TypeGuard[chatlas.ContentToolResult]":
1863+
try:
1864+
from chatlas.types import ContentToolResult
1865+
1866+
return isinstance(val, ContentToolResult)
1867+
except ImportError:
1868+
return False
1869+
1870+
18411871
CHAT_INSTANCES: WeakValueDictionary[str, Chat] = WeakValueDictionary()

pkg-py/src/shinychat/_chat_normalize_chatlas.py

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@
1818
from pydantic import BaseModel, field_serializer, field_validator
1919
from typing_extensions import TypeAliasType
2020

21-
from ._typing_extensions import TypeGuard
22-
2321
if TYPE_CHECKING:
2422
from chatlas.types import ContentToolRequest, ContentToolResult
2523

@@ -365,36 +363,6 @@ def tool_result_display(
365363
return str(x.get_model_value()), "code"
366364

367365

368-
async def hide_corresponding_request(x: "ContentToolResult"):
369-
if x.request is None:
370-
return
371-
372-
session = None
373-
try:
374-
from shiny.session import get_current_session
375-
376-
session = get_current_session()
377-
except Exception:
378-
return
379-
380-
if session is None:
381-
return
382-
383-
await session.send_custom_message(
384-
"shiny-tool-request-hide",
385-
x.request.id, # type: ignore
386-
)
387-
388-
389-
def is_tool_result(val: object) -> "TypeGuard[ContentToolResult]":
390-
try:
391-
from chatlas.types import ContentToolResult
392-
393-
return isinstance(val, ContentToolResult)
394-
except ImportError:
395-
return False
396-
397-
398366
# Tools started getting added to ContentToolRequest staring with 0.11.1
399367
def is_legacy():
400368
import chatlas

0 commit comments

Comments
 (0)