Home / Function/ _stream() — langchain Function Reference

_stream() — langchain Function Reference

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

Entity Profile

Dependency Diagram

graph TD
  d6746b81_ab7c_4b02_8921_f885e52e303e["_stream()"]
  977b57b2_5d0e_bcf4_a43e_b52857105005["ChatAnthropic"]
  d6746b81_ab7c_4b02_8921_f885e52e303e -->|defined in| 977b57b2_5d0e_bcf4_a43e_b52857105005
  954f0cfe_731b_ac1b_0145_b5a3d210030b["_get_request_payload()"]
  d6746b81_ab7c_4b02_8921_f885e52e303e -->|calls| 954f0cfe_731b_ac1b_0145_b5a3d210030b
  e06dbdb3_d54e_6b13_f29e_5cc1f96311b6["_create()"]
  d6746b81_ab7c_4b02_8921_f885e52e303e -->|calls| e06dbdb3_d54e_6b13_f29e_5cc1f96311b6
  8f4b2740_7c71_b933_6989_ad2d7f8a4b42["_tools_in_params()"]
  d6746b81_ab7c_4b02_8921_f885e52e303e -->|calls| 8f4b2740_7c71_b933_6989_ad2d7f8a4b42
  56815eb0_82df_1181_dbf9_0d0e97e40514["_documents_in_params()"]
  d6746b81_ab7c_4b02_8921_f885e52e303e -->|calls| 56815eb0_82df_1181_dbf9_0d0e97e40514
  d0043912_79ce_fb22_d24e_6234ef57df26["_thinking_in_params()"]
  d6746b81_ab7c_4b02_8921_f885e52e303e -->|calls| d0043912_79ce_fb22_d24e_6234ef57df26
  aea38cf0_2da9_3eec_9bfd_04f393036557["_compact_in_params()"]
  d6746b81_ab7c_4b02_8921_f885e52e303e -->|calls| aea38cf0_2da9_3eec_9bfd_04f393036557
  01106d9b_3ac9_a2a5_056c_a55bc89d961b["_make_message_chunk_from_anthropic_event()"]
  d6746b81_ab7c_4b02_8921_f885e52e303e -->|calls| 01106d9b_3ac9_a2a5_056c_a55bc89d961b
  b9fd12bf_ff30_9c6b_c6e6_df17524f5c55["_handle_anthropic_bad_request()"]
  d6746b81_ab7c_4b02_8921_f885e52e303e -->|calls| b9fd12bf_ff30_9c6b_c6e6_df17524f5c55
  style d6746b81_ab7c_4b02_8921_f885e52e303e fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

libs/partners/anthropic/langchain_anthropic/chat_models.py lines 1257–1292

    def _stream(
        self,
        messages: list[BaseMessage],
        stop: list[str] | None = None,
        run_manager: CallbackManagerForLLMRun | None = None,
        *,
        stream_usage: bool | None = None,
        **kwargs: Any,
    ) -> Iterator[ChatGenerationChunk]:
        if stream_usage is None:
            stream_usage = self.stream_usage
        kwargs["stream"] = True
        payload = self._get_request_payload(messages, stop=stop, **kwargs)
        try:
            stream = self._create(payload)
            coerce_content_to_string = (
                not _tools_in_params(payload)
                and not _documents_in_params(payload)
                and not _thinking_in_params(payload)
                and not _compact_in_params(payload)
            )
            block_start_event = None
            for event in stream:
                msg, block_start_event = _make_message_chunk_from_anthropic_event(
                    event,
                    stream_usage=stream_usage,
                    coerce_content_to_string=coerce_content_to_string,
                    block_start_event=block_start_event,
                )
                if msg is not None:
                    chunk = ChatGenerationChunk(message=msg)
                    if run_manager and isinstance(msg.content, str):
                        run_manager.on_llm_new_token(msg.content, chunk=chunk)
                    yield chunk
        except anthropic.BadRequestError as e:
            _handle_anthropic_bad_request(e)

Domain

Subdomains

Frequently Asked Questions

What does _stream() do?
_stream() is a function in the langchain codebase, defined in libs/partners/anthropic/langchain_anthropic/chat_models.py.
Where is _stream() defined?
_stream() is defined in libs/partners/anthropic/langchain_anthropic/chat_models.py at line 1257.
What does _stream() call?
_stream() calls 8 function(s): _compact_in_params, _create, _documents_in_params, _get_request_payload, _handle_anthropic_bad_request, _make_message_chunk_from_anthropic_event, _thinking_in_params, _tools_in_params.

Analyze Your Own Codebase

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

Try Supermodel Free