33
44import anyio
55from anyio .streams .memory import MemoryObjectReceiveStream , MemoryObjectSendStream
6+ from pydantic_core import ValidationError
67from starlette .types import Receive , Scope , Send
78from starlette .websockets import WebSocket
89
@@ -33,10 +34,10 @@ async def websocket_server(scope: Scope, receive: Receive, send: Send):
3334 async def ws_reader ():
3435 try :
3536 async with read_stream_writer :
36- async for message in websocket .iter_json ():
37+ async for msg in websocket .iter_text ():
3738 try :
38- client_message = types .JSONRPCMessage .model_validate ( message )
39- except Exception as exc :
39+ client_message = types .JSONRPCMessage .model_validate_json ( msg )
40+ except ValidationError as exc :
4041 await read_stream_writer .send (exc )
4142 continue
4243
@@ -48,10 +49,8 @@ async def ws_writer():
4849 try :
4950 async with write_stream_reader :
5051 async for message in write_stream_reader :
51- obj = message .model_dump (
52- by_alias = True , mode = "json" , exclude_none = True
53- )
54- await websocket .send_json (obj )
52+ obj = message .model_dump_json (by_alias = True , exclude_none = True )
53+ await websocket .send_text (obj )
5554 except anyio .ClosedResourceError :
5655 await websocket .close ()
5756
0 commit comments