Home / Function/ trace_as_chain_group() — langchain Function Reference

trace_as_chain_group() — langchain Function Reference

Architecture documentation for the trace_as_chain_group() function in manager.py from the langchain codebase.

Entity Profile

Dependency Diagram

graph TD
  e694e06d_9c3a_9620_74b6_743814a492ce["trace_as_chain_group()"]
  ef55be46_0333_682d_8311_b4dd35c3e34c["manager.py"]
  e694e06d_9c3a_9620_74b6_743814a492ce -->|defined in| ef55be46_0333_682d_8311_b4dd35c3e34c
  9afe8ec3_6265_0bfd_b9aa_231ee6084fa3["merge()"]
  9afe8ec3_6265_0bfd_b9aa_231ee6084fa3 -->|calls| e694e06d_9c3a_9620_74b6_743814a492ce
  ba6695e3_66d3_2ef4_027e_436cea7aa24c["configure()"]
  e694e06d_9c3a_9620_74b6_743814a492ce -->|calls| ba6695e3_66d3_2ef4_027e_436cea7aa24c
  1b37e7f7_9f67_e0c5_eb5f_dffa98170394["on_chain_start()"]
  e694e06d_9c3a_9620_74b6_743814a492ce -->|calls| 1b37e7f7_9f67_e0c5_eb5f_dffa98170394
  f1a5b3c9_6772_a478_ad3a_5e2dabf4e2a3["get_child()"]
  e694e06d_9c3a_9620_74b6_743814a492ce -->|calls| f1a5b3c9_6772_a478_ad3a_5e2dabf4e2a3
  3b7be403_b5f2_f25b_2714_6d61955e1332["on_chain_end()"]
  e694e06d_9c3a_9620_74b6_743814a492ce -->|calls| 3b7be403_b5f2_f25b_2714_6d61955e1332
  07a4c44a_3cfb_9b0a_ffde_c1bbfeb4080a["on_chain_error()"]
  e694e06d_9c3a_9620_74b6_743814a492ce -->|calls| 07a4c44a_3cfb_9b0a_ffde_c1bbfeb4080a
  style e694e06d_9c3a_9620_74b6_743814a492ce fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

libs/core/langchain_core/callbacks/manager.py lines 64–136

def trace_as_chain_group(
    group_name: str,
    callback_manager: CallbackManager | None = None,
    *,
    inputs: dict[str, Any] | None = None,
    project_name: str | None = None,
    example_id: str | UUID | None = None,
    run_id: UUID | None = None,
    tags: list[str] | None = None,
    metadata: dict[str, Any] | None = None,
) -> Generator[CallbackManagerForChainGroup, None, None]:
    """Get a callback manager for a chain group in a context manager.

    Useful for grouping different calls together as a single run even if they aren't
    composed in a single chain.

    Args:
        group_name: The name of the chain group.
        callback_manager: The callback manager to use.
        inputs: The inputs to the chain group.
        project_name: The name of the project.
        example_id: The ID of the example.
        run_id: The ID of the run.
        tags: The inheritable tags to apply to all runs.
        metadata: The metadata to apply to all runs.

    !!! note

        Must have `LANGCHAIN_TRACING_V2` env var set to true to see the trace in
        LangSmith.

    Yields:
        The callback manager for the chain group.

    Example:
        ```python
        llm_input = "Foo"
        with trace_as_chain_group("group_name", inputs={"input": llm_input}) as manager:
            # Use the callback manager for the chain group
            res = llm.invoke(llm_input, {"callbacks": manager})
            manager.on_chain_end({"output": res})
        ```
    """
    cb = _get_trace_callbacks(
        project_name, example_id, callback_manager=callback_manager
    )
    cm = CallbackManager.configure(
        inheritable_callbacks=cb,
        inheritable_tags=tags,
        inheritable_metadata=metadata,
    )

    run_manager = cm.on_chain_start({"name": group_name}, inputs or {}, run_id=run_id)
    child_cm = run_manager.get_child()
    group_cm = CallbackManagerForChainGroup(
        child_cm.handlers,
        child_cm.inheritable_handlers,
        child_cm.parent_run_id,
        parent_run_manager=run_manager,
        tags=child_cm.tags,
        inheritable_tags=child_cm.inheritable_tags,
        metadata=child_cm.metadata,
        inheritable_metadata=child_cm.inheritable_metadata,
    )
    try:
        yield group_cm
    except Exception as e:
        if not group_cm.ended:
            run_manager.on_chain_error(e)
        raise
    else:
        if not group_cm.ended:
            run_manager.on_chain_end({})

Domain

Subdomains

Called By

Frequently Asked Questions

What does trace_as_chain_group() do?
trace_as_chain_group() is a function in the langchain codebase, defined in libs/core/langchain_core/callbacks/manager.py.
Where is trace_as_chain_group() defined?
trace_as_chain_group() is defined in libs/core/langchain_core/callbacks/manager.py at line 64.
What does trace_as_chain_group() call?
trace_as_chain_group() calls 5 function(s): configure, get_child, on_chain_end, on_chain_error, on_chain_start.
What calls trace_as_chain_group()?
trace_as_chain_group() is called by 1 function(s): merge.

Analyze Your Own Codebase

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

Try Supermodel Free