{"record":{"id":"f56b9d8253cd2505","repo":"deepset-ai/haystack","slug":"cannot-stream-multiple-responses-please-set-n-1","errorCode":null,"errorMessage":"Cannot stream multiple responses, please set n=1.","messagePattern":"Cannot stream multiple responses, please set n=1\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"haystack/components/generators/chat/openai.py","lineNumber":519,"sourceCode":"        return {\"replies\": completions}\n\n    def _prepare_api_call(  # noqa: PLR0913\n        self,\n        *,\n        messages: list[ChatMessage],\n        streaming_callback: StreamingCallbackT | None = None,\n        generation_kwargs: dict[str, Any] | None = None,\n        tools: ToolsType | None = None,\n        tools_strict: bool | None = None,\n    ) -> dict[str, Any]:\n        # update generation kwargs by merging with the generation kwargs passed to the run method\n        generation_kwargs = {**self.generation_kwargs, **(generation_kwargs or {})}\n\n        is_streaming = streaming_callback is not None\n        num_responses = generation_kwargs.pop(\"n\", 1)\n\n        if is_streaming and num_responses > 1:\n            raise ValueError(\"Cannot stream multiple responses, please set n=1.\")\n        response_format = generation_kwargs.pop(\"response_format\", None)\n\n        # adapt ChatMessage(s) to the format expected by the OpenAI API\n        openai_formatted_messages = [message.to_openai_dict_format() for message in messages]\n\n        flattened_tools = flatten_tools_or_toolsets(tools or self.tools)\n        tools_strict = tools_strict if tools_strict is not None else self.tools_strict\n        _check_duplicate_tool_names(flattened_tools)\n\n        openai_tools = {}\n        if flattened_tools:\n            tool_definitions = []\n            for t in flattened_tools:\n                function_spec = {**t.tool_spec}\n                if tools_strict:\n                    function_spec[\"strict\"] = True\n                    function_spec[\"parameters\"] = _make_schema_strict(function_spec[\"parameters\"])\n                tool_definitions.append({\"type\": \"function\", \"function\": function_spec})","sourceCodeStart":501,"sourceCodeEnd":537,"githubUrl":"https://github.com/deepset-ai/haystack/blob/e318778c9bf60a1963e3b5f451359655dd696c30/haystack/components/generators/chat/openai.py#L501-L537","documentation":"OpenAI's streaming API can only emit one completion per request. haystack enforces this by raising ValueError when a streaming_callback is supplied and generation_kwargs request n > 1 responses simultaneously.","triggerScenarios":"Calling OpenAIChatGenerator.run(..., streaming_callback=cb) while generation_kwargs contains n=2 (or more), either set at init or merged via the per-call generation_kwargs.","commonSituations":"Upgrading code that previously requested multiple candidates non-interactively and adding streaming; a shared generation_kwargs dict carrying n>1 for both streaming and non-streaming paths.","solutions":["Set n=1 (or remove n) when using streaming_callback","Run n parallel non-streaming calls if you need multiple candidates","Only pass streaming_callback for the streaming code path"],"exampleFix":"// before\ngen.run(messages=msgs, generation_kwargs={\"n\": 3}, streaming_callback=cb)\n// after\ngen.run(messages=msgs, generation_kwargs={\"n\": 1}, streaming_callback=cb)","handlingStrategy":"validation","validationCode":"if streaming_callback is not None and kwargs.get(\"n\", 1) > 1:\n    kwargs[\"n\"] = 1","typeGuard":null,"tryCatchPattern":"try:\n    gen.run(messages=msgs, generation_kwargs=gk, streaming_callback=cb)\nexcept ValueError as e:\n    if \"Cannot stream multiple responses\" in str(e):\n        gk[\"n\"] = 1\n        gen.run(messages=msgs, generation_kwargs=gk, streaming_callback=cb)","preventionTips":["Force n=1 whenever streaming","Keep separate kwargs dicts for streaming and non-streaming paths","Assert n==1 before adding streaming_callback"],"tags":["python","haystack","openai","streaming","validation"],"backgroundTag":"streaming-with-multiple-responses","analyzedSha":"e318778c9bf60a1963e3b5f451359655dd696c30","analyzedAt":"2026-08-30T11:45:20.711Z","schemaVersion":2},"datasetVersion":"2026-08-30T13:17:10.514Z"}