def build_factory_websocket_endpoint(
    path: str, endpoint: Callable[..., Any]
) -> Callable[..., Awaitable[Any]]:
    """Build a factory endpoint for WebSocket routes.

    Args:
        path: The path for the route.
        endpoint: LangChain instance factory function.
    """
    chain = compile_chain_factory(endpoint)

    # index 1 of `compile_path` contains path_format output
    model_prefix = compile_model_prefix(compile_path(path)[1], chain)
    request_model = create_request_model(chain, model_prefix)

    async def factory_endpoint(websocket: WebSocket, chain: Chain = Depends(endpoint)):
        callbacks = get_websocket_callbacks(chain, websocket)
        async with WebsocketSession().connect(websocket) as session:
            async for data in session:
                try:
                    await chain.acall(
                        inputs=model_dump(request_model(**data)),
                        callbacks=callbacks,
                    )
                except Exception as e:
                    logger.error(f"langchain error: {e}")
                    await websocket.send_json(
                        dict(
                            data=dict(
                                status=500,
                                detail=HTTPStatusDetail(
                                    code=500,
                                    message="Internal Server Error",
                                ),
                            ),
                            event=Events.ERROR,
                        )
                    )
                await websocket.send_json(dict(data="", event=Events.END))

    return factory_endpoint
Build a factory endpoint for WebSocket routes.

    Args:
        path: The path for the route.
        endpoint: LangChain instance factory function.
    
lanarky/adapters/langchain/utils.py