{"record":{"id":"b49e2ce46584de7b","repo":"run-llama/llama_index","slug":"asyncstreamingresponse-not-supported-in-sync-code","errorCode":null,"errorMessage":"AsyncStreamingResponse not supported in sync code.","messagePattern":"AsyncStreamingResponse not supported in sync code\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"llama-index-core/llama_index/core/query_engine/router_query_engine.py","lineNumber":47,"sourceCode":"from llama_index.core.tools.types import ToolMetadata\nfrom llama_index.core.utils import print_text\n\nlogger = logging.getLogger(__name__)\n\n\ndef combine_responses(\n    summarizer: TreeSummarize, responses: List[RESPONSE_TYPE], query_bundle: QueryBundle\n) -> RESPONSE_TYPE:\n    \"\"\"Combine multiple response from sub-engines.\"\"\"\n    logger.info(\"Combining responses from multiple query engines.\")\n\n    response_strs = []\n    source_nodes = []\n    for response in responses:\n        if isinstance(response, (StreamingResponse, PydanticResponse)):\n            response_obj = response.get_response()\n        elif isinstance(response, AsyncStreamingResponse):\n            raise ValueError(\"AsyncStreamingResponse not supported in sync code.\")\n        else:\n            response_obj = response\n        source_nodes.extend(response_obj.source_nodes)\n        response_strs.append(str(response))\n\n    summary = summarizer.get_response(query_bundle.query_str, response_strs)\n\n    if isinstance(summary, str):\n        return Response(response=summary, source_nodes=source_nodes)\n    elif isinstance(summary, BaseModel):\n        return PydanticResponse(response=summary, source_nodes=source_nodes)\n    elif isinstance(summary, Generator):\n        return StreamingResponse(response_gen=summary, source_nodes=source_nodes)\n    else:\n        return AsyncStreamingResponse(response_gen=summary, source_nodes=source_nodes)\n\n\nasync def acombine_responses(","sourceCodeStart":29,"sourceCodeEnd":65,"githubUrl":"https://github.com/run-llama/llama_index/blob/afd0fef371831f9bda13e5af7167cf4e981278ab/llama-index-core/llama_index/core/query_engine/router_query_engine.py#L29-L65","documentation":"combine_responses() in RouterQueryEngine merges sub-engine responses synchronously. It accepts Response, StreamingResponse, and PydanticResponse, but if a sub-engine returned an AsyncStreamingResponse there is no sync way to consume it, so it raises ValueError immediately.","triggerScenarios":"Calling the sync path of RouterQueryEngine.query() where a multi-selection routes to sub-query engines that return async streaming responses (e.g. streaming sub-engines queried through a sync combine), hitting the isinstance(response, AsyncStreamingResponse) branch in combine_responses.","commonSituations":"Mixing streaming-enabled sub-query engines with the sync RouterQueryEngine.query() API instead of await aquery(), or a custom query engine whose sync query() accidentally returns an async generator-backed streaming response.","solutions":["Call `await router_engine.aquery(...)` instead of the sync `query(...)` when sub-engines are streaming/async","Disable streaming on the sub-query engines (streaming=False) if you must use the sync API","In a custom query engine, ensure _query() returns sync RESPONSE_TYPE objects (Response/StreamingResponse), never AsyncStreamingResponse"],"exampleFix":"// before\nresponse = router_engine.query(\"compare sales and support docs\")\n\n// after\nresponse = await router_engine.aquery(\"compare sales and support docs\")\n# or set streaming=False on sub-engines to keep sync query()","handlingStrategy":"validation","validationCode":"from llama_index.core.response.schema import AsyncStreamingResponse\n\ndef responses_are_sync_safe(responses) -> bool:\n    return not any(isinstance(r, AsyncStreamingResponse) for r in responses)\n\n# or simply: choose the API up front\nasync def run(router, q):\n    return await router.aquery(q)  # async path supports all response types","typeGuard":"from llama_index.core.response.schema import AsyncStreamingResponse\n\ndef is_async_streaming(r) -> bool:\n    \"\"\"True when the response cannot be consumed by sync code.\"\"\"\n    return isinstance(r, AsyncStreamingResponse)","tryCatchPattern":"try:\n    resp = router.query(q)\nexcept ValueError as e:\n    if \"AsyncStreamingResponse not supported in sync code\" in str(e):\n        resp = asyncio.run(router.aquery(q))\n    else:\n        raise","preventionTips":["Use aquery()/await consistently in async applications","Set streaming=False on sub-query engines when the outer caller is synchronous","Never return AsyncStreamingResponse from a custom engine's sync _query()"],"tags":["async","streaming","router","sync-mismatch"],"backgroundTag":null,"analyzedSha":"afd0fef371831f9bda13e5af7167cf4e981278ab","analyzedAt":"2026-08-15T05:42:58.429Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}