LangSmith Spans Reference¶
Helpers for emitting Nevermined-flavored payment spans into a LangSmith trace.
When the optional [langsmith] extra is installed and a LangSmith run is active in the current context (typically because LANGSMITH_TRACING=true is set and the call is inside a traced runnable), the @requires_payment decorator automatically uses these helpers to emit dedicated nvm:verify and nvm:settlement child spans nested under the active tool span.
For non-LangChain code paths (e.g. the FastAPI middleware) the context managers can also be used directly — see the example in verify_span / settlement_span.
All helpers in this module silently no-op when:
- the
langsmithpackage is not installed, or - no LangSmith run tree is active in the current context.
Failures inside this module never propagate out — observability is best-effort and must not interfere with the payment flow.
spans
¶
LangSmith span helpers for Nevermined payment events.
All helpers in this module silently no-op when
- the
langsmithpackage is not installed; or - no LangSmith run tree is active in the current context
(e.g.
LANGSMITH_TRACINGis unset, or the call is not inside a traced runnable).
Failures inside this module never propagate out -- observability is best-effort and must not interfere with the payment flow.
verify_span
¶
verify_span(
plan_ids: list[str],
scheme: Optional[str] = None,
network: Optional[str] = None,
agent_id: Optional[str] = None,
) -> Iterator[Optional[Any]]
Open an nvm:verify child span around a verify call.
Yields the underlying RunTree if LangSmith is active and a parent
run is in scope; yields None otherwise. Always safe to call.
Exceptions raised by the caller's body propagate out unchanged -- the
underlying langsmith.trace context manager sees them via its
__exit__ and records the span as failed before re-raising. Only
errors raised inside the trace SETUP (constructing or entering the
LangSmith context) are swallowed, since those are pure observability
concerns and must never interfere with the payment flow.
Example::
with verify_span(plan_ids=["plan-1"]) as span:
verification = payments.facilitator.verify_permissions(...)
add_metadata(span, build_verify_metadata(
plan_ids=["plan-1"], verification=verification,
))
Source code in payments_py/langsmith/spans.py
settlement_span
¶
Open an nvm:settlement child span around a settle call.
Same semantics as :func:verify_span.
Source code in payments_py/langsmith/spans.py
build_verify_metadata
¶
build_verify_metadata(
plan_ids: list[str],
scheme: Optional[str] = None,
network: Optional[str] = None,
agent_id: Optional[str] = None,
verification: Optional[VerifyResponse] = None,
duration_ms: Optional[float] = None,
token: Optional[str] = None,
) -> dict
Build the nvm.* metadata dict for a verify span. Drops None values.
token is abbreviated via :func:abbreviate_token before being
surfaced as nvm.payment_token so the full credential never ends
up in metadata that we control.
Source code in payments_py/langsmith/spans.py
build_settle_metadata
¶
build_settle_metadata(
settlement: SettleResponse,
plan_ids: list[str],
agent_id: Optional[str] = None,
duration_ms: Optional[float] = None,
token: Optional[str] = None,
) -> dict
Build the nvm.* metadata dict for a settlement span. Drops None values.
token is abbreviated via :func:abbreviate_token before being
surfaced as nvm.payment_token.
Source code in payments_py/langsmith/spans.py
active_run_tree
¶
Return the current LangSmith RunTree, or None if no run is active.
Safe to call regardless of whether langsmith is installed.
Source code in payments_py/langsmith/spans.py
add_metadata
¶
Attach metadata to run_tree, swallowing any error.
No-op if run_tree is None or metadata is empty.