Home / Function/ _aiterate_over_stream() — langchain Function Reference

_aiterate_over_stream() — langchain Function Reference

Architecture documentation for the _aiterate_over_stream() function in chat_models.py from the langchain codebase.

Entity Profile

Dependency Diagram

graph TD
  99d71779_02a3_5ac1_9f57_f8b40ddef80e["_aiterate_over_stream()"]
  19e4be00_71fb_5390_6768_f6e6158f49b4["ChatOllama"]
  99d71779_02a3_5ac1_9f57_f8b40ddef80e -->|defined in| 19e4be00_71fb_5390_6768_f6e6158f49b4
  2e66c349_d5d3_62e8_92b6_0bc2344b7f7b["_achat_stream_with_aggregation()"]
  2e66c349_d5d3_62e8_92b6_0bc2344b7f7b -->|calls| 99d71779_02a3_5ac1_9f57_f8b40ddef80e
  d3156d7d_2212_27d9_1af7_5f566188d094["_astream()"]
  d3156d7d_2212_27d9_1af7_5f566188d094 -->|calls| 99d71779_02a3_5ac1_9f57_f8b40ddef80e
  e9825a4f_e9d8_3008_6f5d_48284410cad7["_acreate_chat_stream()"]
  99d71779_02a3_5ac1_9f57_f8b40ddef80e -->|calls| e9825a4f_e9d8_3008_6f5d_48284410cad7
  b0d70326_371d_ec7e_1902_93fb811b392d["_get_usage_metadata_from_generation_info()"]
  99d71779_02a3_5ac1_9f57_f8b40ddef80e -->|calls| b0d70326_371d_ec7e_1902_93fb811b392d
  22b4b72b_4c0e_fde2_2401_52040de102e0["_get_tool_calls_from_response()"]
  99d71779_02a3_5ac1_9f57_f8b40ddef80e -->|calls| 22b4b72b_4c0e_fde2_2401_52040de102e0
  style 99d71779_02a3_5ac1_9f57_f8b40ddef80e fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

libs/partners/ollama/langchain_ollama/chat_models.py lines 1127–1187

    async def _aiterate_over_stream(
        self,
        messages: list[BaseMessage],
        stop: list[str] | None = None,
        **kwargs: Any,
    ) -> AsyncIterator[ChatGenerationChunk]:
        reasoning = kwargs.get("reasoning", self.reasoning)
        async for stream_resp in self._acreate_chat_stream(messages, stop, **kwargs):
            if not isinstance(stream_resp, str):
                content = (
                    stream_resp["message"]["content"]
                    if "message" in stream_resp and "content" in stream_resp["message"]
                    else ""
                )

                # Warn and skip responses with done_reason: 'load' and empty content
                # These indicate the model was loaded but no actual generation occurred
                is_load_response_with_empty_content = (
                    stream_resp.get("done") is True
                    and stream_resp.get("done_reason") == "load"
                    and not content.strip()
                )

                if is_load_response_with_empty_content:
                    log.warning(
                        "Ollama returned empty response with done_reason='load'. "
                        "This typically indicates the model was loaded but no content "
                        "was generated. Skipping this response."
                    )
                    continue

                if stream_resp.get("done") is True:
                    generation_info = dict(stream_resp)
                    if "model" in generation_info:
                        generation_info["model_name"] = generation_info["model"]
                    generation_info["model_provider"] = "ollama"
                    _ = generation_info.pop("message", None)
                else:
                    generation_info = None

                additional_kwargs = {}
                if (
                    reasoning
                    and "message" in stream_resp
                    and (thinking_content := stream_resp["message"].get("thinking"))
                ):
                    additional_kwargs["reasoning_content"] = thinking_content

                chunk = ChatGenerationChunk(
                    message=AIMessageChunk(
                        content=content,
                        additional_kwargs=additional_kwargs,
                        usage_metadata=_get_usage_metadata_from_generation_info(
                            stream_resp
                        ),
                        tool_calls=_get_tool_calls_from_response(stream_resp),
                    ),
                    generation_info=generation_info,
                )

                yield chunk

Domain

Subdomains

Frequently Asked Questions

What does _aiterate_over_stream() do?
_aiterate_over_stream() is a function in the langchain codebase, defined in libs/partners/ollama/langchain_ollama/chat_models.py.
Where is _aiterate_over_stream() defined?
_aiterate_over_stream() is defined in libs/partners/ollama/langchain_ollama/chat_models.py at line 1127.
What does _aiterate_over_stream() call?
_aiterate_over_stream() calls 3 function(s): _acreate_chat_stream, _get_tool_calls_from_response, _get_usage_metadata_from_generation_info.
What calls _aiterate_over_stream()?
_aiterate_over_stream() is called by 2 function(s): _achat_stream_with_aggregation, _astream.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free