Home / File/ file.py — langchain Source File

file.py — langchain Source File

Architecture documentation for file.py, a python file in the langchain codebase. 7 imports, 0 dependents.

File python CoreAbstractions Serialization 7 imports 1 functions 1 classes

Entity Profile

Dependency Diagram

graph LR
  aef24696_b275_601e_ecac_84177a75ea65["file.py"]
  b6ee5de5_719a_eeb5_1e11_e9c63bc22ef8["pathlib"]
  aef24696_b275_601e_ecac_84177a75ea65 --> b6ee5de5_719a_eeb5_1e11_e9c63bc22ef8
  8e2034b7_ceb8_963f_29fc_2ea6b50ef9b3["typing"]
  aef24696_b275_601e_ecac_84177a75ea65 --> 8e2034b7_ceb8_963f_29fc_2ea6b50ef9b3
  91721f45_4909_e489_8c1f_084f8bd87145["typing_extensions"]
  aef24696_b275_601e_ecac_84177a75ea65 --> 91721f45_4909_e489_8c1f_084f8bd87145
  b19a8b7e_fbee_95b1_65b8_509a1ed3cad7["langchain_core._api"]
  aef24696_b275_601e_ecac_84177a75ea65 --> b19a8b7e_fbee_95b1_65b8_509a1ed3cad7
  f3bc7443_c889_119d_0744_aacc3620d8d2["langchain_core.callbacks"]
  aef24696_b275_601e_ecac_84177a75ea65 --> f3bc7443_c889_119d_0744_aacc3620d8d2
  75b56223_6cf5_b347_8586_de20156157a1["langchain_core.utils.input"]
  aef24696_b275_601e_ecac_84177a75ea65 --> 75b56223_6cf5_b347_8586_de20156157a1
  80d582c5_7cc3_ac96_2742_3dbe1cbd4e2b["langchain_core.agents"]
  aef24696_b275_601e_ecac_84177a75ea65 --> 80d582c5_7cc3_ac96_2742_3dbe1cbd4e2b
  style aef24696_b275_601e_ecac_84177a75ea65 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

"""Callback handler that writes to a file."""

from __future__ import annotations

from pathlib import Path
from typing import TYPE_CHECKING, Any, TextIO, cast

from typing_extensions import Self, override

from langchain_core._api import warn_deprecated
from langchain_core.callbacks import BaseCallbackHandler
from langchain_core.utils.input import print_text

if TYPE_CHECKING:
    from langchain_core.agents import AgentAction, AgentFinish


_GLOBAL_DEPRECATION_WARNED = False


class FileCallbackHandler(BaseCallbackHandler):
    """Callback handler that writes to a file.

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

    Examples:
        Using as a context manager (recommended):

        ```python
        with FileCallbackHandler("output.txt") as handler:
            # Use handler with your chain/agent
            chain.invoke(inputs, config={"callbacks": [handler]})
        ```

        Direct instantiation (deprecated):

        ```python
        handler = FileCallbackHandler("output.txt")
        # File remains open until handler is garbage collected
        try:
            chain.invoke(inputs, config={"callbacks": [handler]})
        finally:
            handler.close()  # Explicit cleanup recommended
        ```

    Args:
        filename: The file path to write to.
        mode: The file open mode. Defaults to `'a'` (append).
        color: Default color for text output.

    !!! note

        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.

    """

    def __init__(
// ... (208 more lines)

Subdomains

Functions

Dependencies

  • langchain_core._api
  • langchain_core.agents
  • langchain_core.callbacks
  • langchain_core.utils.input
  • pathlib
  • typing
  • typing_extensions

Frequently Asked Questions

What does file.py do?
file.py is a source file in the langchain codebase, written in python. It belongs to the CoreAbstractions domain, Serialization subdomain.
What functions are defined in file.py?
file.py defines 1 function(s): langchain_core.
What does file.py depend on?
file.py imports 7 module(s): langchain_core._api, langchain_core.agents, langchain_core.callbacks, langchain_core.utils.input, pathlib, typing, typing_extensions.
Where is file.py in the architecture?
file.py is located at libs/core/langchain_core/callbacks/file.py (domain: CoreAbstractions, subdomain: Serialization, directory: libs/core/langchain_core/callbacks).

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free