Skip to content

log

gepa.optimize_anything.log(*args: Any, sep: str = ' ', end: str = '\n') -> None

Log diagnostic information during evaluation without printing to stdout.

Has the same calling convention as print(). Output is captured per-evaluator-call (thread-safe) and automatically included in side_info under the "log" key.

Must only be called inside an evaluator function passed to optimize_anything. Calling it outside that context will emit a warning and the output will be silently discarded.

For child threads spawned by your evaluator, propagate the log context via :func:get_log_context / :func:set_log_context.

Usage::

import gepa.optimize_anything as oa
oa.log("Landing distance:", distance, "meters")
Source code in gepa/optimize_anything.py
def log(*args: Any, sep: str = " ", end: str = "\n") -> None:
    """Log diagnostic information during evaluation without printing to stdout.

    Has the same calling convention as ``print()``.  Output is captured
    per-evaluator-call (thread-safe) and automatically included in
    side_info under the ``"log"`` key.

    Must only be called inside an evaluator function passed to
    ``optimize_anything``.  Calling it outside that context will emit a
    warning and the output will be silently discarded.

    For child threads spawned by your evaluator, propagate the log context
    via :func:`get_log_context` / :func:`set_log_context`.

    Usage::

        import gepa.optimize_anything as oa
        oa.log("Landing distance:", distance, "meters")
    """
    ctx = _get_log_context()
    if ctx is None:
        warnings.warn(
            "oa.log() called outside of an evaluator function. "
            "Output will be discarded. Only call oa.log() inside your evaluator, "
            "or propagate the log context to child threads via "
            "oa.get_log_context() / oa.set_log_context().",
            stacklevel=2,
        )
        return
    text = sep.join(str(a) for a in args) + end
    ctx.write(text)