feat: add lifecycle hooks for agent execution flow#1959
Draft
wingding12 wants to merge 1 commit intohuggingface:mainfrom
Draft
feat: add lifecycle hooks for agent execution flow#1959wingding12 wants to merge 1 commit intohuggingface:mainfrom
wingding12 wants to merge 1 commit intohuggingface:mainfrom
Conversation
Adds lifecycle callbacks that fire at specific points during agent
execution, enabling finer-grained control than step_callbacks (which
only fire after steps complete).
New lifecycle events:
- MODEL_OUTPUT: Triggered after LLM generates output, before parsing.
Use case: Send to TTS service before code execution starts.
- PRE_EXECUTION: Triggered before code/tool execution, after parsing.
Use case: Human-in-the-loop approval, custom validation.
Usage:
```python
def on_model_output(event: ModelOutputEvent, agent):
tts_service.queue(event.model_output)
def on_pre_execution(event: PreExecutionEvent, agent):
if "dangerous" in (event.code_action or ""):
raise ValueError("Blocked!")
agent = CodeAgent(
tools=[...],
model=model,
lifecycle_callbacks={
LifecycleEvent.MODEL_OUTPUT: on_model_output,
LifecycleEvent.PRE_EXECUTION: on_pre_execution,
}
)
```
Changes:
- memory.py: Add LifecycleEvent enum, ModelOutputEvent/PreExecutionEvent
dataclasses, and LifecycleCallbackRegistry class
- agents.py: Add lifecycle_callbacks parameter to MultiStepAgent,
trigger events in CodeAgent._step_stream and ToolCallingAgent._step_stream
- test_agents.py: Add comprehensive tests for lifecycle hooks
Fixes huggingface#1883
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #1883 - Add lifecycle hooks for CodeAgent execution flow
Adds lifecycle callbacks that fire at specific points during agent execution, enabling finer-grained control than
step_callbacks(which only fire after steps complete). This addresses the use case of needing to execute callbacks at different points in the execution flow, such as sending LLM output to a TTS service before code execution.New Lifecycle Events
MODEL_OUTPUTPRE_EXECUTIONUsage Example
Files Changed
src/smolagents/memory.pyLifecycleEventenum,ModelOutputEvent/PreExecutionEventdataclasses,LifecycleCallbackRegistryclasssrc/smolagents/agents.pylifecycle_callbacksparameter toMultiStepAgent, trigger events inCodeAgent._step_streamandToolCallingAgent._step_streamtests/test_agents.pyTest plan