Skip to Content

LangChain API Reference (Python)

Method signatures, option types and extended log fields for the LangChain and LangGraph callback path. For a guide-level introduction, see LangChain.

langchain()

Returns a LangChain BaseCallbackHandler that you pass into callbacks. Async because LangChain is imported on demand. Create one callback per request so each request gets its own trace.

The callback is synchronous and works with both invoke() and ainvoke().

Signature

async def langchain( self, options: LangChainCallbackOptions | None = None, ) -> BaseCallbackHandler

Parameters

NameTypeDefaultDescription
options.rolestr'unspecified'Overrides the role set on AiAccountability.init() for this callback.
options.trace_idstrautoCustom trace id. Defaults to the root run’s run_id.
options.on_errorCallableNoneCalled when an internal handler error occurs. If omitted, the error and its entry are dropped silently.

Returns

BaseCallbackHandler (awaitable factory).

Example

from sarek import AiAccountability, LangChainCallbackOptions sarek = await AiAccountability.init(name='loan-desk') callback = await sarek.langchain( LangChainCallbackOptions( role='credit-assessor', trace_id='request-2026-06-16-4821', on_error=lambda err, context: app_logger.error( 'sarek callback error', extra={'handler': context['handler'], 'run_id': context['run_id']}, ), ) )

langgraph()

Alias for langchain(). Use it for LangGraph apps; the callback behaves identically.

Signature

async def langgraph( self, options: LangChainCallbackOptions | None = None, ) -> BaseCallbackHandler

Event types

The callback logs five event types. The discriminator is event_type:

EventStart handlerInput hashOutput hash
llmchat model or legacy LLM startthe prompt messages or stringsthe generated text
tooltool startthe tool inputthe tool output
retrieverretriever startthe querydocument metadata only, never content
chainchain startthe chain inputsthe chain outputs
errorLLM, tool or chain errorreused from the matching startempty

Extended LogEntry fields

On top of the standard LogEntry fields, the callback path populates these:

FieldSourceNotes
event_typethe handler that firedllm, tool, retriever, chain or error
outcomethe handler that firedsuccess, or error on a failed call
trace_idthe root run’s run_idequals LangSmith’s trace_id; ties one request together
run_idthe callback’s run id for this eventunique per event
parent_run_idthe callback’s parent run idNone on the root; reconstructs the call tree
chain_namethe serialized chain idchain events
tool_namethe tool nametool events
tool_call_idthe model’s tool-call idlinks a tool event to the call that requested it
error_classthe exception class nameerror events
error_hashSHA3-256 of the error messagethe raw message is never stored
model_paramsconfigured from the invocation params, actual from the resultllm events only; see below
thread_idmetadata.thread_idLangGraph; None for plain LangChain
langgraph_nodemetadata.langgraph_nodeLangGraph; None otherwise
langgraph_stepmetadata.langgraph_stepLangGraph; None otherwise
checkpoint_nsmetadata.checkpoint_nsLangGraph; None otherwise
tags, metadatapassed through from the callbackcarry deployer context, end-user id, jurisdiction

model_params: configured against actual

On llm events, model_params separates what you asked for from what the provider reported:

  • configured holds temperature, max_tokens and top_p, read from the invocation parameters LangChain builds for the call. These are the same values LangSmith logs.
  • actual holds the provider’s own output block, framed as what the provider actually reported. Many providers leave most of it empty, which is expected.

Retriever output is metadata only

For retriever events the output hash is computed over the id and source of each retrieved document, never the document content. The page content never leaves the SDK. If you need the content for retention, keep it yourself.

Fields that differ on this path

A few standard LogEntry fields do not carry their usual meaning when the entry comes from a callback:

FieldOn the callback path
prediction_idempty; LangChain does not surface a provider response id here
citationsnot populated; this is a Perplexity-only field on the direct path
status_codealways 200. Use outcome for success or failure, not the status code
environmentalways None in Python
Last updated on