LangChain Reference home pageLangChain ReferenceLangChain Reference
  • GitHub
  • Main Docs
Deep Agents
LangChain
LangGraph
Integrations
LangSmith
  • Overview
    • Overview
    • Caches
    • Callbacks
    • Documents
    • Document loaders
    • Embeddings
    • Exceptions
    • Language models
    • Serialization
    • Output parsers
    • Prompts
    • Rate limiters
    • Retrievers
    • Runnables
    • Utilities
    • Vector stores
    MCP Adapters
    Standard Tests
    Text Splitters
    ⌘I

    LangChain Assistant

    Ask a question to get started

    Enter to send•Shift+Enter new line

    Menu

    OverviewCachesCallbacksDocumentsDocument loadersEmbeddingsExceptionsLanguage modelsSerializationOutput parsersPromptsRate limitersRetrieversRunnablesUtilitiesVector stores
    MCP Adapters
    Standard Tests
    Text Splitters
    Language
    Theme
    Pythonlangchain-corecallbacksfileFileCallbackHandler
    Classā—Since v0.1

    FileCallbackHandler

    Callback handler that writes to a file.

    This handler supports both context manager usage (recommended) and direct instantiation (deprecated) for backwards compatibility.

    Copy
    FileCallbackHandler(
      self,
      filename: str,
      mode: str = 'a',
      color: str | None = None
    )

    Bases

    BaseCallbackHandler

    When not used as a context manager, a deprecation warning will be issued on first use. The file will be opened immediately in __init__ and closed in __del__ or when close() is called explicitly.

    Parameters

    NameTypeDescription
    filename*str

    The file path to write to.

    modestr
    Default:'a'

    The file open mode. Defaults to 'a' (append).

    colorstr | None
    Default:None

    Default color for text output.

    Constructors

    constructor
    __init__
    NameType
    filenamestr
    modestr
    colorstr | None

    Attributes

    attribute
    filename: filename
    attribute
    mode: mode
    attribute
    color: color
    attribute
    file: TextIO

    Methods

    method
    close

    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.

    method
    on_chain_start

    Print that we are entering a chain.

    method
    on_chain_end

    Print that we finished a chain.

    method
    on_agent_action

    Handle agent action by writing the action log.

    method
    on_tool_end

    Handle tool end by writing the output with optional prefixes.

    method
    on_text

    Handle text output.

    method
    on_agent_finish

    Handle agent finish by writing the finish log.

    Inherited fromBaseCallbackHandler

    Attributes

    Araise_error: bool
    —

    Whether to raise an error if an exception occurs.

    Arun_inline: boolAignore_llm: bool
    —

    Whether to ignore LLM callbacks.

    Aignore_retry: bool
    —

    Whether to ignore retry callbacks.

    Aignore_chain: bool
    —

    Whether to ignore chain callbacks.

    Aignore_agent: bool
    —

    Whether to ignore agent callbacks.

    Aignore_retriever: bool
    —

    Whether to ignore retriever callbacks.

    Aignore_chat_model: bool
    —

    Whether to ignore chat model callbacks.

    Aignore_custom_event: bool
    —

    Ignore custom event.

    Inherited fromLLMManagerMixin

    Methods

    Mon_llm_new_token
    —

    Run on new output token.

    Mon_llm_end
    —

    End a trace for a model run.

    Mon_llm_error
    —

    Handle an error for an LLM run.

    Inherited fromChainManagerMixin

    Methods

    Mon_chain_error
    —

    Handle an error for a chain run.

    Inherited fromToolManagerMixin

    Methods

    Mon_tool_error
    —

    Run when tool errors.

    Inherited fromRetrieverManagerMixin

    Methods

    Mon_retriever_error
    —

    Run when Retriever errors.

    Mon_retriever_end
    —

    Run when Retriever ends running.

    Inherited fromCallbackManagerMixin

    Methods

    Mon_llm_start
    —

    Start a trace for a (non-chat model) LLM run.

    Mon_chat_model_start
    —

    Start a trace for a chat model run.

    Mon_retriever_start
    —

    Run when Retriever starts running.

    Mon_tool_start
    —

    Start a trace for a tool run.

    Inherited fromRunManagerMixin

    Methods

    Mon_retry
    —

    Run on retry.

    Mon_custom_event
    —

    Generate a custom astream event.

    View source on GitHub