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
  7f210df0_4f6a_343c_b4fe_f942048ea776["stream()"]
  d009a608_c505_bd50_7200_0de8a69ba4b7["BaseChatModel"]
  7f210df0_4f6a_343c_b4fe_f942048ea776 -->|defined in| d009a608_c505_bd50_7200_0de8a69ba4b7
  85ef976a_84b2_6421_f697_db0f396b2444["_should_stream()"]
  7f210df0_4f6a_343c_b4fe_f942048ea776 -->|calls| 85ef976a_84b2_6421_f697_db0f396b2444
  f5ae3987_a3e6_8941_d427_2ceec1002585["invoke()"]
  7f210df0_4f6a_343c_b4fe_f942048ea776 -->|calls| f5ae3987_a3e6_8941_d427_2ceec1002585
  b0e1e167_44cd_1c63_6e71_0caf683fc904["_convert_input()"]
  7f210df0_4f6a_343c_b4fe_f942048ea776 -->|calls| b0e1e167_44cd_1c63_6e71_0caf683fc904
  5c320356_b8cd_92a3_38a3_2878a3c460d0["_get_invocation_params()"]
  7f210df0_4f6a_343c_b4fe_f942048ea776 -->|calls| 5c320356_b8cd_92a3_38a3_2878a3c460d0
  47283f0d_d8e7_addf_ea32_c0ecefe3d97c["_get_ls_params()"]
  7f210df0_4f6a_343c_b4fe_f942048ea776 -->|calls| 47283f0d_d8e7_addf_ea32_c0ecefe3d97c
  6c8e0d50_abee_649f_cdb5_db6e45f078e3["_stream()"]
  7f210df0_4f6a_343c_b4fe_f942048ea776 -->|calls| 6c8e0d50_abee_649f_cdb5_db6e45f078e3
  5f652461_f9fa_fdc2_d659_cde32ef53f66["_format_ls_structured_output()"]
  7f210df0_4f6a_343c_b4fe_f942048ea776 -->|calls| 5f652461_f9fa_fdc2_d659_cde32ef53f66
  f1b77769_1c98_a324_e709_fd921b433e56["_format_for_tracing()"]
  7f210df0_4f6a_343c_b4fe_f942048ea776 -->|calls| f1b77769_1c98_a324_e709_fd921b433e56
  3aa65704_c798_ec7b_b231_6600cb1a6a44["_gen_info_and_msg_metadata()"]
  7f210df0_4f6a_343c_b4fe_f942048ea776 -->|calls| 3aa65704_c798_ec7b_b231_6600cb1a6a44
  0a0401bd_a59c_7ac5_1c91_a5f406b3cdc6["_generate_response_from_error()"]
  7f210df0_4f6a_343c_b4fe_f942048ea776 -->|calls| 0a0401bd_a59c_7ac5_1c91_a5f406b3cdc6
  style 7f210df0_4f6a_343c_b4fe_f942048ea776 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

libs/core/langchain_core/language_models/chat_models.py lines 480–603

    def stream(
        self,
        input: LanguageModelInput,
        config: RunnableConfig | None = None,
        *,
        stop: list[str] | None = None,
        **kwargs: Any,
    ) -> Iterator[AIMessageChunk]:
        if not self._should_stream(async_api=False, **{**kwargs, "stream": True}):
            # Model doesn't implement streaming, so use default implementation
            yield cast(
                "AIMessageChunk",
                self.invoke(input, config=config, stop=stop, **kwargs),
            )
        else:
            config = ensure_config(config)
            messages = self._convert_input(input).to_messages()
            ls_structured_output_format = kwargs.pop(
                "ls_structured_output_format", None
            ) or kwargs.pop("structured_output_format", None)
            ls_structured_output_format_dict = _format_ls_structured_output(
                ls_structured_output_format
            )

            params = self._get_invocation_params(stop=stop, **kwargs)
            options = {"stop": stop, **kwargs, **ls_structured_output_format_dict}
            inheritable_metadata = {
                **(config.get("metadata") or {}),
                **self._get_ls_params(stop=stop, **kwargs),
            }
            callback_manager = CallbackManager.configure(
                config.get("callbacks"),
                self.callbacks,
                self.verbose,
                config.get("tags"),
                self.tags,
                inheritable_metadata,
                self.metadata,
            )
            (run_manager,) = callback_manager.on_chat_model_start(
                self._serialized,
                [_format_for_tracing(messages)],
                invocation_params=params,
                options=options,
                name=config.get("run_name"),
                run_id=config.pop("run_id", None),
                batch_size=1,
            )

            chunks: list[ChatGenerationChunk] = []

            if self.rate_limiter:
                self.rate_limiter.acquire(blocking=True)

            try:
                input_messages = _normalize_messages(messages)
                run_id = "-".join((LC_ID_PREFIX, str(run_manager.run_id)))
                yielded = False
                index = -1
                index_type = ""
                for chunk in self._stream(input_messages, stop=stop, **kwargs):
                    if chunk.message.id is None:
                        chunk.message.id = run_id
                    chunk.message.response_metadata = _gen_info_and_msg_metadata(chunk)
                    if self.output_version == "v1":
                        # Overwrite .content with .content_blocks
                        chunk.message = _update_message_content_to_blocks(
                            chunk.message, "v1"
                        )
                        for block in cast(
                            "list[types.ContentBlock]", chunk.message.content
                        ):
                            if block["type"] != index_type:
                                index_type = block["type"]
                                index += 1
                            if "index" not in block:
                                block["index"] = index
                    run_manager.on_llm_new_token(
                        cast("str", chunk.message.content), chunk=chunk
                    )
                    chunks.append(chunk)

Subdomains

Frequently Asked Questions

What does stream() do?
stream() is a function in the langchain codebase, defined in libs/core/langchain_core/language_models/chat_models.py.
Where is stream() defined?
stream() is defined in libs/core/langchain_core/language_models/chat_models.py at line 480.
What does stream() call?
stream() calls 10 function(s): _convert_input, _format_for_tracing, _format_ls_structured_output, _gen_info_and_msg_metadata, _generate_response_from_error, _get_invocation_params, _get_ls_params, _should_stream, and 2 more.

Analyze Your Own Codebase

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

Try Supermodel Free