Home / Function/ _generate() — langchain Function Reference

_generate() — langchain Function Reference

Architecture documentation for the _generate() function in model.py from the langchain codebase.

Entity Profile

Dependency Diagram

graph TD
  b3112488_5244_cc1c_84da_f065ee64f731["_generate()"]
  67d9a14e_be69_a367_fd3c_cdb7f78dfa76["FakeToolCallingModel"]
  b3112488_5244_cc1c_84da_f065ee64f731 -->|defined in| 67d9a14e_be69_a367_fd3c_cdb7f78dfa76
  style b3112488_5244_cc1c_84da_f065ee64f731 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

libs/langchain_v1/tests/unit_tests/agents/model.py lines 29–67

    def _generate(
        self,
        messages: list[BaseMessage],
        stop: list[str] | None = None,
        run_manager: CallbackManagerForLLMRun | None = None,
        **kwargs: Any,
    ) -> ChatResult:
        """Top Level call."""
        is_native = kwargs.get("response_format")

        if self.tool_calls:
            if is_native:
                tool_calls = (
                    self.tool_calls[self.index] if self.index < len(self.tool_calls) else []
                )
            else:
                tool_calls = self.tool_calls[self.index % len(self.tool_calls)]
        else:
            tool_calls = []

        if is_native and not tool_calls:
            if isinstance(self.structured_response, BaseModel):
                content_obj = self.structured_response.model_dump()
            elif is_dataclass(self.structured_response) and not isinstance(
                self.structured_response, type
            ):
                content_obj = asdict(self.structured_response)
            elif isinstance(self.structured_response, dict):
                content_obj = self.structured_response
            message = AIMessage(content=json.dumps(content_obj), id=str(self.index))
        else:
            messages_string = "-".join([m.text for m in messages])
            message = AIMessage(
                content=messages_string,
                id=str(self.index),
                tool_calls=tool_calls.copy(),
            )
        self.index += 1
        return ChatResult(generations=[ChatGeneration(message=message)])

Domain

Subdomains

Frequently Asked Questions

What does _generate() do?
_generate() is a function in the langchain codebase, defined in libs/langchain_v1/tests/unit_tests/agents/model.py.
Where is _generate() defined?
_generate() is defined in libs/langchain_v1/tests/unit_tests/agents/model.py at line 29.

Analyze Your Own Codebase

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

Try Supermodel Free