{"record":{"id":"0f1841ad0fe0440a","repo":"langchain-ai/langchain","slug":"no-generations-found-in-stream","errorCode":null,"errorMessage":"No generations found in stream.","messagePattern":"No generations found in stream\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"libs/core/langchain_core/language_models/chat_models.py","lineNumber":224,"sourceCode":"def generate_from_stream(stream: Iterator[ChatGenerationChunk]) -> ChatResult:\n    \"\"\"Generate from a stream.\n\n    Args:\n        stream: Iterator of `ChatGenerationChunk`.\n\n    Raises:\n        ValueError: If no generations are found in the stream.\n\n    Returns:\n        Chat result.\n\n    \"\"\"\n    generation = next(stream, None)\n    if generation:\n        generation += list(stream)\n    if generation is None:\n        msg = \"No generations found in stream.\"\n        raise ValueError(msg)\n    return ChatResult(\n        generations=[\n            ChatGeneration(\n                message=message_chunk_to_message(generation.message),\n                generation_info=generation.generation_info,\n            )\n        ]\n    )\n\n\nasync def agenerate_from_stream(\n    stream: AsyncIterator[ChatGenerationChunk],\n) -> ChatResult:\n    \"\"\"Async generate from a stream.\n\n    Args:\n        stream: AsyncIterator of `ChatGenerationChunk`.\n","sourceCodeStart":206,"sourceCodeEnd":242,"githubUrl":"https://github.com/langchain-ai/langchain/blob/e32fa9a52eab3b61ad7a45399bfde59b3e580fc4/libs/core/langchain_core/language_models/chat_models.py#L206-L242","documentation":"`ValueError` from `generate_from_stream`: the first `next(stream, None)` returned a falsy value, meaning the generation stream was empty. The helper is used when a chat model is asked to build a `ChatResult` from a stream of `ChatGenerationChunk`s and at least one chunk is required.","triggerScenarios":"Calling a model with `stream=True` (or the `_generate_with_cache` fallback path calling `generate_from_stream`) when the underlying stream iterator yields no chunks, or yields only a chunk that is falsy (e.g. an empty `ChatGenerationChunk`).","commonSituations":"Custom `_stream` implementations that return without yielding; providers returning an empty response body (200 with no chunks); network proxies stripping SSE events; `stream_mode` misconfiguration producing zero chunks.","solutions":["Verify the provider actually returns chunks for the request (test the raw API call with `curl` or the provider SDK directly).","Fix custom `_stream` overrides to always yield at least one `ChatGenerationChunk` (see `fake_chat_models` for the expected pattern).","If the provider can legitimately return empty streams, catch `ValueError` and return an empty `AIMessage` instead.","Check for proxy/gateway interference that drops `text/event-stream` payloads."],"exampleFix":"# before\nasync def _stream(self, messages, **kw):\n    if not messages:\n        return\n    yield ...  # early return leaves stream empty\n\n# after\nasync def _stream(self, messages, **kw):\n    yield ChatGenerationChunk(message=AIMessageChunk(content=self._call(messages)))","handlingStrategy":"validation","validationCode":"first = next(stream, None)\nif first is None:\n    raise ValueError(\"provider returned an empty stream\")\ngeneration = first + list(stream)","typeGuard":null,"tryCatchPattern":"try:\n    result = generate_from_stream(stream)\nexcept ValueError as e:\n    if \"No generations found\" in str(e):\n        # retry once or degrade to non-streaming call\n        result = model.generate([messages])\n    else:\n        raise","preventionTips":["Test custom `_stream` implementations with at least one chunk yielded.","Log the count of chunks received from providers during development.","Degrade to non-streaming `_generate` when a provider is known to return empty streams."],"tags":["streaming","empty-stream","validation"],"backgroundTag":null,"analyzedSha":"e32fa9a52eab3b61ad7a45399bfde59b3e580fc4","analyzedAt":"2026-08-14T18:42:09.092Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}