🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.
TypeScript analyticsautogenevaluationlangchainlarge-language-modelsllama-indexllmllm-evaluationllm-observabilityllmopsmonitoringobservabilityopen-sourceopenaiplaygroundprompt-engineeringprompt-managementself-hostedycombinator
fix(otel): drop non-numeric cost details on the direct events path (#18962)
Tobias Wochingerf6afd7ccf5f6
diff --git a/packages/shared/src/server/otel/OtelIngestionProcessor.ts b/packages/shared/src/server/otel/OtelIngestionProcessor.tsindex a5c0502175..aa8af23410 100644--- a/packages/shared/src/server/otel/OtelIngestionProcessor.ts+++ b/packages/shared/src/server/otel/OtelIngestionProcessor.ts@@ -3455,27 +3455,45 @@ export class OtelIngestionProcessor {private extractCostDetails(attributes: Record<string, unknown>,context: { spanId: string; traceId: string; source: string },- ): Record<string, unknown> {+ ): Record<string, number> {const fromAttribute = this.parseJsonObjectAttribute(attributes,LangfuseOtelSpanAttributes.OBSERVATION_COST_DETAILS,context,);- if (fromAttribute !== null) return fromAttribute;+ if (fromAttribute !== null) return this.toNumericEntries(fromAttribute);const genAiUsageCost = attributes["gen_ai.usage.cost"];if (genAiUsageCost != null && genAiUsageCost !== "") {- return { total: genAiUsageCost };+ return this.toNumericEntries({ total: genAiUsageCost });}const openInferenceTotalCost = attributes["llm.cost.total"];if (openInferenceTotalCost != null && openInferenceTotalCost !== "") {- return { total: openInferenceTotalCost };+ return this.toNumericEntries({ total: openInferenceTotalCost });}return {};}+ /** Keeps finite numbers and non-blank numeric strings (as numbers); drops other entries such as currency. */+ private toNumericEntries(+ details: Record<string, unknown>,+ ): Record<string, number> {+ const result: Record<string, number> = {};+ for (const [key, value] of Object.entries(details)) {+ const numeric = this.toFiniteNumber(value);+ if (Number.isFinite(numeric)) result[key] = numeric;+ }+ return result;+ }++ private toFiniteNumber(value: unknown): number {+ if (typeof value === "number") return value;+ if (typeof value === "string" && value.trim() !== "") return Number(value);+ return NaN;+ }+private extractCompletionStartTime(attributes: Record<string, unknown>,startTimeISO?: string,diff --git a/worker/src/queues/__tests__/otelToObservationForEval.test.ts b/worker/src/queues/__tests__/otelToObservationForEval.test.tsindex 32aa379eb3..fe75ec7fdb 100644--- a/worker/src/queues/__tests__/otelToObservationForEval.test.ts+++ b/worker/src/queues/__tests__/otelToObservationForEval.test.ts@@ -1871,6 +1871,16 @@ describe("OTEL to ObservationForEval Schema Validation", () => {expectedUsage: { input: 100, output: 200 },expectedCost: {},},+ {+ name: "non-numeric cost entries",+ usage: null,+ cost: {+ stringValue:+ '{"total":0.01,"input":"0.004","output":-0.002,"currency":"USD"}',+ },+ expectedUsage: {},+ expectedCost: { total: 0.01, input: 0.004, output: -0.002 },+ },])("should handle $name without data corruption",async ({ usage, cost, expectedUsage, expectedCost }) => {