Home / Function/ transform() — langchain Function Reference

transform() — langchain Function Reference

Architecture documentation for the transform() function in base.py from the langchain codebase.

Function python LangChainCore Runnables calls 2 called by 1

Entity Profile

Dependency Diagram

graph TD
  ec8ef184_f283_14ce_8075_7b1a5a02aeee["transform()"]
  4a62481c_02cb_a5de_1833_50669d5351a6["Runnable"]
  ec8ef184_f283_14ce_8075_7b1a5a02aeee -->|defined in| 4a62481c_02cb_a5de_1833_50669d5351a6
  8b0e1fa6_22b1_9cf9_e19a_e82720511fb4["transform()"]
  8b0e1fa6_22b1_9cf9_e19a_e82720511fb4 -->|calls| ec8ef184_f283_14ce_8075_7b1a5a02aeee
  66bfa0c6_f60c_67bd_cbc1_53b945547106["stream()"]
  ec8ef184_f283_14ce_8075_7b1a5a02aeee -->|calls| 66bfa0c6_f60c_67bd_cbc1_53b945547106
  f619bab3_d5a5_2218_b91d_c55d57073715["transform()"]
  ec8ef184_f283_14ce_8075_7b1a5a02aeee -->|calls| f619bab3_d5a5_2218_b91d_c55d57073715
  style ec8ef184_f283_14ce_8075_7b1a5a02aeee fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

libs/core/langchain_core/runnables/base.py lines 1519–1562

    def transform(
        self,
        input: Iterator[Input],
        config: RunnableConfig | None = None,
        **kwargs: Any | None,
    ) -> Iterator[Output]:
        """Transform inputs to outputs.

        Default implementation of transform, which buffers input and calls `astream`.

        Subclasses must override this method if they can start producing output while
        input is still being generated.

        Args:
            input: An iterator of inputs to the `Runnable`.
            config: The config to use for the `Runnable`.
            **kwargs: Additional keyword arguments to pass to the `Runnable`.

        Yields:
            The output of the `Runnable`.

        """
        final: Input
        got_first_val = False

        for ichunk in input:
            # The default implementation of transform is to buffer input and
            # then call stream.
            # It'll attempt to gather all input into a single chunk using
            # the `+` operator.
            # If the input is not addable, then we'll assume that we can
            # only operate on the last chunk,
            # and we'll iterate until we get to the last chunk.
            if not got_first_val:
                final = ichunk
                got_first_val = True
            else:
                try:
                    final = final + ichunk  # type: ignore[operator]
                except TypeError:
                    final = ichunk

        if got_first_val:
            yield from self.stream(final, config, **kwargs)

Domain

Subdomains

Called By

Frequently Asked Questions

What does transform() do?
transform() is a function in the langchain codebase, defined in libs/core/langchain_core/runnables/base.py.
Where is transform() defined?
transform() is defined in libs/core/langchain_core/runnables/base.py at line 1519.
What does transform() call?
transform() calls 2 function(s): stream, transform.
What calls transform()?
transform() is called by 1 function(s): transform.

Analyze Your Own Codebase

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

Try Supermodel Free