Home / Function/ _should_stream() — langchain Function Reference

_should_stream() — langchain Function Reference

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

Entity Profile

Dependency Diagram

graph TD
  eea23e6b_3cbd_585d_793d_cad1199b446e["_should_stream()"]
  48aa29b8_65e7_522f_a445_a441eeb6baff["BaseChatModel"]
  eea23e6b_3cbd_585d_793d_cad1199b446e -->|defined in| 48aa29b8_65e7_522f_a445_a441eeb6baff
  2c835c4d_6d4b_1dc6_028c_27710e69a32a["stream()"]
  2c835c4d_6d4b_1dc6_028c_27710e69a32a -->|calls| eea23e6b_3cbd_585d_793d_cad1199b446e
  9d211053_b2a3_2915_b514_1f332955694a["astream()"]
  9d211053_b2a3_2915_b514_1f332955694a -->|calls| eea23e6b_3cbd_585d_793d_cad1199b446e
  7f734836_7715_c31c_80ea_e14410d37d70["_generate_with_cache()"]
  7f734836_7715_c31c_80ea_e14410d37d70 -->|calls| eea23e6b_3cbd_585d_793d_cad1199b446e
  d2cd58bb_554b_a407_049a_8aaad431d049["_agenerate_with_cache()"]
  d2cd58bb_554b_a407_049a_8aaad431d049 -->|calls| eea23e6b_3cbd_585d_793d_cad1199b446e
  style eea23e6b_3cbd_585d_793d_cad1199b446e fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

libs/core/langchain_core/language_models/chat_models.py lines 439–477

    def _should_stream(
        self,
        *,
        async_api: bool,
        run_manager: CallbackManagerForLLMRun
        | AsyncCallbackManagerForLLMRun
        | None = None,
        **kwargs: Any,
    ) -> bool:
        """Determine if a given model call should hit the streaming API."""
        sync_not_implemented = type(self)._stream == BaseChatModel._stream  # noqa: SLF001
        async_not_implemented = type(self)._astream == BaseChatModel._astream  # noqa: SLF001

        # Check if streaming is implemented.
        if (not async_api) and sync_not_implemented:
            return False
        # Note, since async falls back to sync we check both here.
        if async_api and async_not_implemented and sync_not_implemented:
            return False

        # Check if streaming has been disabled on this instance.
        if self.disable_streaming is True:
            return False
        # We assume tools are passed in via "tools" kwarg in all models.
        if self.disable_streaming == "tool_calling" and kwargs.get("tools"):
            return False

        # Check if a runtime streaming flag has been passed in.
        if "stream" in kwargs:
            return bool(kwargs["stream"])

        if "streaming" in self.model_fields_set:
            streaming_value = getattr(self, "streaming", None)
            if isinstance(streaming_value, bool):
                return streaming_value

        # Check if any streaming callback handlers have been passed in.
        handlers = run_manager.handlers if run_manager else []
        return any(isinstance(h, _StreamingCallbackHandler) for h in handlers)

Domain

Subdomains

Frequently Asked Questions

What does _should_stream() do?
_should_stream() is a function in the langchain codebase, defined in libs/core/langchain_core/language_models/chat_models.py.
Where is _should_stream() defined?
_should_stream() is defined in libs/core/langchain_core/language_models/chat_models.py at line 439.
What calls _should_stream()?
_should_stream() is called by 4 function(s): _agenerate_with_cache, _generate_with_cache, astream, stream.

Analyze Your Own Codebase

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

Try Supermodel Free