Home / Function/ return_stopped_response() — langchain Function Reference

return_stopped_response() — langchain Function Reference

Architecture documentation for the return_stopped_response() function in agent.py from the langchain codebase.

Entity Profile

Dependency Diagram

graph TD
  91ad3c4b_7d4d_167d_7a4e_117cb9d64e89["return_stopped_response()"]
  c55759c0_8c32_142f_44ff_2733a473d3e2["Agent"]
  91ad3c4b_7d4d_167d_7a4e_117cb9d64e89 -->|defined in| c55759c0_8c32_142f_44ff_2733a473d3e2
  57ca57d7_9f7c_b17a_52ef_4ff0afd3568a["_call()"]
  57ca57d7_9f7c_b17a_52ef_4ff0afd3568a -->|calls| 91ad3c4b_7d4d_167d_7a4e_117cb9d64e89
  3a34a1ff_e9ef_cf10_6260_9d4fa1b12d85["_acall()"]
  3a34a1ff_e9ef_cf10_6260_9d4fa1b12d85 -->|calls| 91ad3c4b_7d4d_167d_7a4e_117cb9d64e89
  17ffb6b2_fdd9_29f3_ea89_3211cf54065f["return_stopped_response()"]
  91ad3c4b_7d4d_167d_7a4e_117cb9d64e89 -->|calls| 17ffb6b2_fdd9_29f3_ea89_3211cf54065f
  7f20b33a_2c0c_9512_1e6e_da874c4dc01f["parse()"]
  91ad3c4b_7d4d_167d_7a4e_117cb9d64e89 -->|calls| 7f20b33a_2c0c_9512_1e6e_da874c4dc01f
  style 91ad3c4b_7d4d_167d_7a4e_117cb9d64e89 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

libs/langchain/langchain_classic/agents/agent.py lines 920–973

    def return_stopped_response(
        self,
        early_stopping_method: str,
        intermediate_steps: list[tuple[AgentAction, str]],
        **kwargs: Any,
    ) -> AgentFinish:
        """Return response when agent has been stopped due to max iterations.

        Args:
            early_stopping_method: Method to use for early stopping.
            intermediate_steps: Steps the LLM has taken to date,
                along with observations.
            **kwargs: User inputs.

        Returns:
            Agent finish object.

        Raises:
            ValueError: If `early_stopping_method` is not in ['force', 'generate'].
        """
        if early_stopping_method == "force":
            # `force` just returns a constant string
            return AgentFinish(
                {"output": "Agent stopped due to iteration limit or time limit."},
                "",
            )
        if early_stopping_method == "generate":
            # Generate does one final forward pass
            thoughts = ""
            for action, observation in intermediate_steps:
                thoughts += action.log
                thoughts += (
                    f"\n{self.observation_prefix}{observation}\n{self.llm_prefix}"
                )
            # Adding to the previous steps, we now tell the LLM to make a final pred
            thoughts += (
                "\n\nI now need to return a final answer based on the previous steps:"
            )
            new_inputs = {"agent_scratchpad": thoughts, "stop": self._stop}
            full_inputs = {**kwargs, **new_inputs}
            full_output = self.llm_chain.predict(**full_inputs)
            # We try to extract a final answer
            parsed_output = self.output_parser.parse(full_output)
            if isinstance(parsed_output, AgentFinish):
                # If we can extract, we send the correct stuff
                return parsed_output
            # If we can extract, but the tool is not the final tool,
            # we just return the full output
            return AgentFinish({"output": full_output}, full_output)
        msg = (
            "early_stopping_method should be one of `force` or `generate`, "
            f"got {early_stopping_method}"
        )
        raise ValueError(msg)

Subdomains

Called By

Frequently Asked Questions

What does return_stopped_response() do?
return_stopped_response() is a function in the langchain codebase, defined in libs/langchain/langchain_classic/agents/agent.py.
Where is return_stopped_response() defined?
return_stopped_response() is defined in libs/langchain/langchain_classic/agents/agent.py at line 920.
What does return_stopped_response() call?
return_stopped_response() calls 2 function(s): parse, return_stopped_response.
What calls return_stopped_response()?
return_stopped_response() is called by 2 function(s): _acall, _call.

Analyze Your Own Codebase

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

Try Supermodel Free