FileCallbackHandler

Struct FileCallbackHandler 

Source
pub struct FileCallbackHandler {
    pub color: Option<String>,
    /* private fields */
}
Expand description

Callback Handler that writes to a file.

This handler supports writing callback output to a file. It can be used to log chain execution to a file for debugging or auditing purposes.

§Example

use agent_chain_core::callbacks::FileCallbackHandler;

// Using with mode string (recommended, matches Python API)
let handler = FileCallbackHandler::with_mode("output.txt", "a")?;

// Using with append boolean
let handler = FileCallbackHandler::new("output.txt", false)?;

Fields§

§color: Option<String>

The color to use for the text (not used for file output but kept for API compatibility).

Implementations§

Source§

impl FileCallbackHandler

Source

pub fn new<P: AsRef<Path>>(filename: P, append: bool) -> Result<Self>

Create a new FileCallbackHandler.

§Arguments
  • filename - The path to the output file.
  • append - Whether to append to the file or truncate it.
§Returns

A Result containing the FileCallbackHandler or an IO error.

Source

pub fn with_mode<P: AsRef<Path>>(filename: P, mode: &str) -> Result<Self>

Create a new FileCallbackHandler with a specific file mode.

This matches the Python API more closely.

§Arguments
  • filename - Path to the output file.
  • mode - File open mode (e.g., “w”, “a”, “x”). Defaults to “a”.
§Returns

A Result containing the FileCallbackHandler or an IO error.

Source

pub fn with_color<P: AsRef<Path>>( filename: P, mode: &str, color: impl Into<String>, ) -> Result<Self>

Create a new FileCallbackHandler with a specific color.

§Arguments
  • filename - Path to the output file.
  • mode - File open mode (e.g., “w”, “a”). Defaults to “a”.
  • color - Default text color for output.
Source

pub fn filename(&self) -> &str

Get the file path (filename).

Source

pub fn mode(&self) -> &str

Get the file mode.

Source

pub fn close(&mut self)

Close the file if it’s open.

This method is safe to call multiple times and will only close the file if it’s currently open.

Source

pub fn flush(&mut self) -> Result<()>

Flush the writer.

Trait Implementations§

Source§

impl BaseCallbackHandler for FileCallbackHandler

Source§

fn name(&self) -> &str

Get a unique name for this handler. Note: This is a Rust-specific addition for debugging purposes.
Source§

fn raise_error(&self) -> bool

Whether to raise an error if an exception occurs.
Source§

fn run_inline(&self) -> bool

Whether to run the callback inline.
Source§

fn ignore_llm(&self) -> bool

Whether to ignore LLM callbacks.
Source§

fn ignore_retry(&self) -> bool

Whether to ignore retry callbacks.
Source§

fn ignore_chain(&self) -> bool

Whether to ignore chain callbacks.
Source§

fn ignore_agent(&self) -> bool

Whether to ignore agent callbacks.
Source§

fn ignore_retriever(&self) -> bool

Whether to ignore retriever callbacks.
Source§

fn ignore_chat_model(&self) -> bool

Whether to ignore chat model callbacks.
Source§

fn ignore_custom_event(&self) -> bool

Whether to ignore custom events.
Source§

impl CallbackManagerMixin for FileCallbackHandler

Source§

fn on_chain_start( &mut self, serialized: &HashMap<String, Value>, _inputs: &HashMap<String, Value>, _run_id: Uuid, _parent_run_id: Option<Uuid>, _tags: Option<&[String]>, metadata: Option<&HashMap<String, Value>>, )

Run when a chain starts running.
Source§

fn on_llm_start( &mut self, serialized: &HashMap<String, Value>, prompts: &[String], run_id: Uuid, parent_run_id: Option<Uuid>, tags: Option<&[String]>, metadata: Option<&HashMap<String, Value>>, )

Run when LLM starts running.
Source§

fn on_chat_model_start( &mut self, serialized: &HashMap<String, Value>, messages: &[Vec<BaseMessage>], run_id: Uuid, parent_run_id: Option<Uuid>, tags: Option<&[String]>, metadata: Option<&HashMap<String, Value>>, )

Run when a chat model starts running.
Source§

fn on_retriever_start( &mut self, serialized: &HashMap<String, Value>, query: &str, run_id: Uuid, parent_run_id: Option<Uuid>, tags: Option<&[String]>, metadata: Option<&HashMap<String, Value>>, )

Run when the Retriever starts running.
Source§

fn on_tool_start( &mut self, serialized: &HashMap<String, Value>, input_str: &str, run_id: Uuid, parent_run_id: Option<Uuid>, tags: Option<&[String]>, metadata: Option<&HashMap<String, Value>>, inputs: Option<&HashMap<String, Value>>, )

Run when the tool starts running.
Source§

impl ChainManagerMixin for FileCallbackHandler

Source§

fn on_agent_action( &mut self, action: &Value, _run_id: Uuid, _parent_run_id: Option<Uuid>, _color: Option<&str>, )

Handle agent action by writing the action log.

Source§

fn on_agent_finish( &mut self, finish: &Value, _run_id: Uuid, _parent_run_id: Option<Uuid>, _color: Option<&str>, )

Handle agent finish by writing the finish log.

Source§

fn on_chain_end( &mut self, _outputs: &HashMap<String, Value>, _run_id: Uuid, _parent_run_id: Option<Uuid>, )

Run when chain ends running.
Source§

fn on_chain_error( &mut self, error: &dyn Error, run_id: Uuid, parent_run_id: Option<Uuid>, )

Run when chain errors.
Source§

impl Debug for FileCallbackHandler

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Drop for FileCallbackHandler

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl LLMManagerMixin for FileCallbackHandler

Source§

fn on_llm_new_token( &mut self, token: &str, run_id: Uuid, parent_run_id: Option<Uuid>, chunk: Option<&Value>, )

Run on new output token. Only available when streaming is enabled.
Source§

fn on_llm_end( &mut self, response: &ChatResult, run_id: Uuid, parent_run_id: Option<Uuid>, )

Run when LLM ends running.
Source§

fn on_llm_error( &mut self, error: &dyn Error, run_id: Uuid, parent_run_id: Option<Uuid>, )

Run when LLM errors.
Source§

impl RetrieverManagerMixin for FileCallbackHandler

Source§

fn on_retriever_error( &mut self, error: &dyn Error, run_id: Uuid, parent_run_id: Option<Uuid>, )

Run when Retriever errors.
Source§

fn on_retriever_end( &mut self, documents: &[Value], run_id: Uuid, parent_run_id: Option<Uuid>, )

Run when Retriever ends running.
Source§

impl RunManagerMixin for FileCallbackHandler

Source§

fn on_text( &mut self, text: &str, _run_id: Uuid, _parent_run_id: Option<Uuid>, _color: Option<&str>, end: &str, )

Handle text output.

Source§

fn on_retry( &mut self, retry_state: &dyn Any, run_id: Uuid, parent_run_id: Option<Uuid>, )

Run on a retry event.
Source§

fn on_custom_event( &mut self, name: &str, data: &dyn Any, run_id: Uuid, tags: Option<&[String]>, metadata: Option<&HashMap<String, Value>>, )

Override to define a handler for a custom event.
Source§

impl ToolManagerMixin for FileCallbackHandler

Source§

fn on_tool_end( &mut self, output: &str, _run_id: Uuid, _parent_run_id: Option<Uuid>, _color: Option<&str>, observation_prefix: Option<&str>, llm_prefix: Option<&str>, )

Handle tool end by writing the output.

Source§

fn on_tool_error( &mut self, error: &dyn Error, run_id: Uuid, parent_run_id: Option<Uuid>, )

Run when tool errors.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more