The reference implementation.
pip install spanforge — zero required dependencies, all 15 spanforge namespaces, a full CLI, and integrations for OpenAI, LangChain, LlamaIndex, CrewAI, and Datadog.
Get started in one command.
Install the base SDK with no required dependencies. Add optional extras for the integrations and export targets you need.
Verify the installation:
spanforge --version # spanforge 1.0.8 [spanforge-Full-2.0]
Optional extras
| Extra | What it adds |
|---|---|
| spanforge[jsonschema] | Enables runtime schema validation of every emitted event against the published JSON Schema. |
| spanforge[openai] | Auto-instrumentation for the openai Python client — wraps chat completions, embeddings, and responses. |
| spanforge[http] | OTLP/HTTP export and webhook export targets. |
| spanforge[pydantic] | Pydantic v2 model support for typed payload validation. |
| spanforge[otel] | Full OpenTelemetry SDK integration — exports spans as OTLP proto via grpc or http. |
| spanforge[kafka] | Apache Kafka export backend for streaming event pipelines. |
| spanforge[langchain] | LangChain callback handler — instruments chain, tool, and LLM calls automatically. |
| spanforge[llamaindex] | LlamaIndex event handler — instruments query, retrieval, and LLM calls. |
| spanforge[crewai] | CrewAI task-and-agent lifecycle instrumentation. |
| spanforge[datadog] | Datadog exporter — ships events to DD APM as custom spans. |
| spanforge[all] | Installs all optional extras in a single command. |
Your first event.
Every event in the spanforge standard is an Event object with three required arguments: event_type, source, and payload.
from spanforge import Event, EventType
# Minimal event
event = Event(
event_type=EventType.TRACE_SPAN_COMPLETED,
source="my-service@0.1.0",
payload={"span_name": "summarise", "status": "ok"},
)
# Event is ready to emit or inspect
print(event.event_id) # ULID e.g. 01HZQSN7PQVR2K4M0BXJD3GSTA
print(event.timestamp) # ISO-8601 UTC
print(event.trace_id) # W3C-compatible 128-bit hexTyped namespace payloads
Use the typed payload classes from each namespace for full schema validation and IDE autocompletion. The classes map 1-to-1 to the published JSON Schema.
from spanforge import Event, EventType
from spanforge.namespaces.trace import SpanPayload, TokenUsage, ModelInfo, GenAISystem
event = Event(
event_type=EventType.TRACE_SPAN_COMPLETED,
source="spanforge@1.0.0",
payload=SpanPayload(
span_name="summarise_document",
span_kind="LLM",
status="ok",
duration_ms=830,
token_usage=TokenUsage(input_tokens=411, output_tokens=128, total_tokens=539),
model_info=ModelInfo(system=GenAISystem.OPENAI, name="gpt-4o"),
).to_dict(),
tags=["prod", "summarisation"],
)
# Export to OTLP
from spanforge.export import OTLPExporter
exporter = OTLPExporter(endpoint="http://localhost:4317")
exporter.export(event)Works with your existing stack.
Install the relevant extra and add one line of code. spanforge events are emitted automatically — you retain full compliance instrumentation without rewriting your existing code.
OpenAI
One-line auto-instrumentation for the openai Python client. Wraps chat completions, embeddings, and the Responses API. Emits llm.trace.span and llm.cost.* events automatically.
LangChain
Drop-in callback handler. Instruments chain invocations, tool calls, and LLM calls. Compatible with LangChain v0.1 and v0.2.
LlamaIndex
Event handler for the LlamaIndex instrumentation API. Captures query, retrieval, embedding, and LLM events with full trace context.
CrewAI
Task and agent lifecycle instrumentation for CrewAI. Captures agent run start/end, task assignment, and tool delegation events.
Datadog
Export spanforge span events as custom Datadog APM spans. Compatible with the Datadog Agent and the dd-trace-py client.
OpenTelemetry
Full OTLP bridge. Converts spanforge span events to OpenTelemetry proto-compatible dicts and exports via gRPC or HTTP. Works with any OTEL-compatible backend.
Operational tooling included.
The spanforge CLI is installed automatically with the SDK. Use it for health checks, schema validation, audit-chain verification, compliance evidence generation, and PII scanning.
# End-to-end health check spanforge check # [1/5] Config ............. OK # [2/5] Event creation ..... OK # [3/5] Schema validation .. OK # [4/5] Export pipeline .... OK # [5/5] Trace store ........ OK # All checks passed. # Validate a JSONL event stream spanforge validate production-events.jsonl # Scan for PII before export spanforge scan production-events.jsonl # Generate compliance evidence package spanforge compliance production-events.jsonl --framework eu-ai-act
SDK instrumentation linter.
The built-in linter parses your Python source with ast and runs five checks to catch common instrumentation mistakes before they reach production. Integrates natively with flake8 and ruff.
Inspect. Validate. Ship.
Use SpanForge Debug to inspect and replay compliance traces, and SpanForge Validate to enforce schema compliance in your CI pipeline.