diff --git a/examples/coagents-qa-text/agent/greeting_agent/demo.py b/examples/coagents-qa-text/agent/greeting_agent/demo.py index 979e529aef..86b7ea4e6e 100644 --- a/examples/coagents-qa-text/agent/greeting_agent/demo.py +++ b/examples/coagents-qa-text/agent/greeting_agent/demo.py @@ -22,7 +22,7 @@ sdk = CopilotKitRemoteEndpoint( ], ) -add_fastapi_endpoint(app, sdk, "/copilotkit") +add_fastapi_endpoint(app, sdk, "/copilotkit", use_thread_pool=False) # add new route for health check @app.get("/health") diff --git a/sdk-python/copilotkit/integrations/fastapi.py b/sdk-python/copilotkit/integrations/fastapi.py index 63898aa5d7..d3194c4207 100644 --- a/sdk-python/copilotkit/integrations/fastapi.py +++ b/sdk-python/copilotkit/integrations/fastapi.py @@ -26,10 +26,13 @@ def add_fastapi_endpoint( prefix: str, *, max_workers: int = 10, + use_thread_pool: bool = True, ): """Add FastAPI endpoint with configurable ThreadPoolExecutor size""" executor = ThreadPoolExecutor(max_workers=max_workers) + print(f"use_thread_pool {use_thread_pool}") + def run_handler_in_thread(request: Request, sdk: CopilotKitRemoteEndpoint): # Run the handler coroutine in the event loop loop = asyncio.new_event_loop() @@ -37,9 +40,11 @@ def add_fastapi_endpoint( return loop.run_until_complete(handler(request, sdk)) async def make_handler(request: Request): - loop = asyncio.get_event_loop() - future = loop.run_in_executor(executor, run_handler_in_thread, request, sdk) - return await future + if use_thread_pool: + loop = asyncio.get_event_loop() + future = loop.run_in_executor(executor, run_handler_in_thread, request, sdk) + return await future + return await handler(request, sdk) # Ensure the prefix starts with a slash and remove trailing slashes normalized_prefix = '/' + prefix.strip('/')