flatten() — langchain Function Reference
Architecture documentation for the flatten() function in llm_result.py from the langchain codebase.
Entity Profile
Dependency Diagram
graph TD 355ab337_7937_6907_7112_0037c8f34822["flatten()"] be2a68ad_6ac6_f078_3bbc_62ebfc7db505["LLMResult"] 355ab337_7937_6907_7112_0037c8f34822 -->|defined in| be2a68ad_6ac6_f078_3bbc_62ebfc7db505 style 355ab337_7937_6907_7112_0037c8f34822 fill:#6366f1,stroke:#818cf8,color:#fff
Relationship Graph
Source Code
libs/core/langchain_core/outputs/llm_result.py lines 59–93
def flatten(self) -> list[LLMResult]:
"""Flatten generations into a single list.
Unpack `list[list[Generation]] -> list[LLMResult]` where each returned
`LLMResult` contains only a single `Generation`. If token usage information is
available, it is kept only for the `LLMResult` corresponding to the top-choice
`Generation`, to avoid over-counting of token usage downstream.
Returns:
List of `LLMResult` objects where each returned `LLMResult` contains a
single `Generation`.
"""
llm_results = []
for i, gen_list in enumerate(self.generations):
# Avoid double counting tokens in OpenAICallback
if i == 0:
llm_results.append(
LLMResult(
generations=[gen_list],
llm_output=self.llm_output,
)
)
else:
if self.llm_output is not None:
llm_output = deepcopy(self.llm_output)
llm_output["token_usage"] = {}
else:
llm_output = None
llm_results.append(
LLMResult(
generations=[gen_list],
llm_output=llm_output,
)
)
return llm_results
Domain
Subdomains
Source
Frequently Asked Questions
What does flatten() do?
flatten() is a function in the langchain codebase, defined in libs/core/langchain_core/outputs/llm_result.py.
Where is flatten() defined?
flatten() is defined in libs/core/langchain_core/outputs/llm_result.py at line 59.
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free