Home / File/ stdout.py — langchain Source File

stdout.py — langchain Source File

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

File python CoreAbstractions RunnableInterface 6 imports 2 functions 2 classes

Entity Profile

Dependency Diagram

graph LR
  59f19799_80e1_28d9_4d71_5c10f6e0cdef["stdout.py"]
  7025b240_fdc3_cf68_b72f_f41dac94566b["json"]
  59f19799_80e1_28d9_4d71_5c10f6e0cdef --> 7025b240_fdc3_cf68_b72f_f41dac94566b
  cfe2bde5_180e_e3b0_df2b_55b3ebaca8e7["collections.abc"]
  59f19799_80e1_28d9_4d71_5c10f6e0cdef --> cfe2bde5_180e_e3b0_df2b_55b3ebaca8e7
  8e2034b7_ceb8_963f_29fc_2ea6b50ef9b3["typing"]
  59f19799_80e1_28d9_4d71_5c10f6e0cdef --> 8e2034b7_ceb8_963f_29fc_2ea6b50ef9b3
  59d7001f_fb28_1819_31fc_7fb0380a8b32["langchain_core.tracers.base"]
  59f19799_80e1_28d9_4d71_5c10f6e0cdef --> 59d7001f_fb28_1819_31fc_7fb0380a8b32
  17e2fb09_6b0f_338f_1319_77bc43602969["langchain_core.tracers.schemas"]
  59f19799_80e1_28d9_4d71_5c10f6e0cdef --> 17e2fb09_6b0f_338f_1319_77bc43602969
  75b56223_6cf5_b347_8586_de20156157a1["langchain_core.utils.input"]
  59f19799_80e1_28d9_4d71_5c10f6e0cdef --> 75b56223_6cf5_b347_8586_de20156157a1
  style 59f19799_80e1_28d9_4d71_5c10f6e0cdef fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

"""Tracers that print to the console."""

import json
from collections.abc import Callable
from typing import Any

from langchain_core.tracers.base import BaseTracer
from langchain_core.tracers.schemas import Run
from langchain_core.utils.input import get_bolded_text, get_colored_text

MILLISECONDS_IN_SECOND = 1000


def try_json_stringify(obj: Any, fallback: str) -> str:
    """Try to stringify an object to JSON.

    Args:
        obj: Object to stringify.
        fallback: Fallback string to return if the object cannot be stringified.

    Returns:
        A JSON string if the object can be stringified, otherwise the fallback string.
    """
    try:
        return json.dumps(obj, indent=2, ensure_ascii=False)
    except Exception:
        return fallback


def elapsed(run: Any) -> str:
    """Get the elapsed time of a run.

    Args:
        run: any object with a `start_time` and `end_time` attribute.

    Returns:
        A string with the elapsed time in seconds or milliseconds if time is less than a
            second.

    """
    elapsed_time = run.end_time - run.start_time
    seconds = elapsed_time.total_seconds()
    if seconds < 1:
        return f"{seconds * MILLISECONDS_IN_SECOND:.0f}ms"
    return f"{seconds:.2f}s"


class FunctionCallbackHandler(BaseTracer):
    """Tracer that calls a function with a single str parameter."""

    name: str = "function_callback_handler"
    """The name of the tracer.

    This is used to identify the tracer in the logs.
    """

    def __init__(self, function: Callable[[str], None], **kwargs: Any) -> None:
        """Create a `FunctionCallbackHandler`.

        Args:
// ... (146 more lines)

Subdomains

Dependencies

  • collections.abc
  • json
  • langchain_core.tracers.base
  • langchain_core.tracers.schemas
  • langchain_core.utils.input
  • typing

Frequently Asked Questions

What does stdout.py do?
stdout.py is a source file in the langchain codebase, written in python. It belongs to the CoreAbstractions domain, RunnableInterface subdomain.
What functions are defined in stdout.py?
stdout.py defines 2 function(s): elapsed, try_json_stringify.
What does stdout.py depend on?
stdout.py imports 6 module(s): collections.abc, json, langchain_core.tracers.base, langchain_core.tracers.schemas, langchain_core.utils.input, typing.
Where is stdout.py in the architecture?
stdout.py is located at libs/core/langchain_core/tracers/stdout.py (domain: CoreAbstractions, subdomain: RunnableInterface, directory: libs/core/langchain_core/tracers).

Analyze Your Own Codebase

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

Try Supermodel Free