__iter__() — langchain Function Reference
Architecture documentation for the __iter__() function in agent_iterator.py from the langchain codebase.
Entity Profile
Dependency Diagram
graph TD a9a06b32_02d8_cc0e_a2b9_4c37ea88936c["__iter__()"] 490b6bce_9d4c_017c_89ae_b13ea74223ea["AgentExecutorIterator"] a9a06b32_02d8_cc0e_a2b9_4c37ea88936c -->|defined in| 490b6bce_9d4c_017c_89ae_b13ea74223ea c1255a97_00b6_d464_ae57_ba7c07bdee0f["reset()"] a9a06b32_02d8_cc0e_a2b9_4c37ea88936c -->|calls| c1255a97_00b6_d464_ae57_ba7c07bdee0f 81f35366_672d_40de_3b62_7c9531baa585["update_iterations()"] a9a06b32_02d8_cc0e_a2b9_4c37ea88936c -->|calls| 81f35366_672d_40de_3b62_7c9531baa585 d5f8bce5_089f_ce88_6683_139f191b2eb6["_process_next_step_output()"] a9a06b32_02d8_cc0e_a2b9_4c37ea88936c -->|calls| d5f8bce5_089f_ce88_6683_139f191b2eb6 dd6b6d4a_9a73_3cb5_87b9_01faaa9d94e2["_stop()"] a9a06b32_02d8_cc0e_a2b9_4c37ea88936c -->|calls| dd6b6d4a_9a73_3cb5_87b9_01faaa9d94e2 style a9a06b32_02d8_cc0e_a2b9_4c37ea88936c fill:#6366f1,stroke:#818cf8,color:#fff
Relationship Graph
Source Code
libs/langchain/langchain_classic/agents/agent_iterator.py lines 173–235
def __iter__(self: AgentExecutorIterator) -> Iterator[AddableDict]:
"""Create an async iterator for the `AgentExecutor`."""
logger.debug("Initialising AgentExecutorIterator")
self.reset()
callback_manager = CallbackManager.configure(
self.callbacks,
self.agent_executor.callbacks,
self.agent_executor.verbose,
self.tags,
self.agent_executor.tags,
self.metadata,
self.agent_executor.metadata,
)
run_manager = callback_manager.on_chain_start(
dumpd(self.agent_executor),
self.inputs,
self.run_id,
name=self.run_name,
)
try:
while self.agent_executor._should_continue( # noqa: SLF001
self.iterations,
self.time_elapsed,
):
# take the next step: this plans next action, executes it,
# yielding action and observation as they are generated
next_step_seq: NextStepOutput = []
for chunk in self.agent_executor._iter_next_step( # noqa: SLF001
self.name_to_tool_map,
self.color_mapping,
self.inputs,
self.intermediate_steps,
run_manager,
):
next_step_seq.append(chunk)
# if we're yielding actions, yield them as they come
# do not yield AgentFinish, which will be handled below
if self.yield_actions:
if isinstance(chunk, AgentAction):
yield AddableDict(actions=[chunk], messages=chunk.messages)
elif isinstance(chunk, AgentStep):
yield AddableDict(steps=[chunk], messages=chunk.messages)
# convert iterator output to format handled by _process_next_step_output
next_step = self.agent_executor._consume_next_step(next_step_seq) # noqa: SLF001
# update iterations and time elapsed
self.update_iterations()
# decide if this is the final output
output = self._process_next_step_output(next_step, run_manager)
is_final = "intermediate_step" not in output
# yield the final output always
# for backwards compat, yield int. output if not yielding actions
if not self.yield_actions or is_final:
yield output
# if final output reached, stop iteration
if is_final:
return
except BaseException as e:
run_manager.on_chain_error(e)
raise
# if we got here means we exhausted iterations or time
yield self._stop(run_manager)
Domain
Subdomains
Source
Frequently Asked Questions
What does __iter__() do?
__iter__() is a function in the langchain codebase, defined in libs/langchain/langchain_classic/agents/agent_iterator.py.
Where is __iter__() defined?
__iter__() is defined in libs/langchain/langchain_classic/agents/agent_iterator.py at line 173.
What does __iter__() call?
__iter__() calls 4 function(s): _process_next_step_output, _stop, reset, update_iterations.
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free