langfuse generation spans with token usage; switch llm to vllm (qwen2.5-coder-7b on mcrndeb)

This commit is contained in:
2026-06-03 10:33:22 -03:00
parent bd34c8c5ce
commit 61494362a3
7 changed files with 103 additions and 25 deletions

View File

@@ -55,9 +55,14 @@ def span(
as_type: str = "span",
input: Any = None,
metadata: dict[str, Any] | None = None,
model: str | None = None,
) -> Iterator[Any]:
"""Open a Langfuse observation; yields the span object or a no-op.
Pass `model="<id>"` when `as_type="generation"` so Langfuse can label
the trace and compute cost when it knows the model's pricing. Token
usage is reported by the body via `span.update(usage_details=...)`.
Exceptions raised by the *body* of the `with` block propagate up
untouched. Only Langfuse's own setup failures fall through to a no-op
span — we never want to mask a real error.
@@ -66,10 +71,16 @@ def span(
if lf is None:
yield _NullSpan()
return
kwargs: dict[str, Any] = {
"name": name,
"as_type": as_type,
"input": input,
"metadata": metadata or {},
}
if model is not None:
kwargs["model"] = model
try:
cm = lf.start_as_current_observation(
name=name, as_type=as_type, input=input, metadata=metadata or {}
)
cm = lf.start_as_current_observation(**kwargs)
except Exception as e:
logger.warning("langfuse setup for %s failed: %s", name, e)
yield _NullSpan()