🪢 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(cost-tracking): null values in OpenAI usage schema (#6184)
Hassieb Pakzad845faee38574
diff --git a/fern/apis/server/definition/ingestion.yml b/fern/apis/server/definition/ingestion.ymlindex b0878f06ff..664e2d14b0 100644--- a/fern/apis/server/definition/ingestion.yml+++ b/fern/apis/server/definition/ingestion.yml@@ -400,8 +400,8 @@ types:prompt_tokens: integercompletion_tokens: integertotal_tokens: integer- prompt_tokens_details: optional<map<string, integer>>- completion_tokens_details: optional<map<string, integer>>+ prompt_tokens_details: optional<map<string, optional<integer>>>+ completion_tokens_details: optional<map<string, optional<integer>>>OpenAIResponseUsageSchema:docs: OpenAI Usage schema from Response API@@ -409,8 +409,8 @@ types:input_tokens: integeroutput_tokens: integertotal_tokens: integer- input_tokens_details: optional<map<string, integer>>- output_tokens_details: optional<map<string, integer>>+ input_tokens_details: optional<map<string, optional<integer>>>+ output_tokens_details: optional<map<string, optional<integer>>>UsageDetails:discriminated: falsediff --git a/packages/shared/src/server/ingestion/types.ts b/packages/shared/src/server/ingestion/types.tsindex 89103221e6..38b92607c4 100644--- a/packages/shared/src/server/ingestion/types.ts+++ b/packages/shared/src/server/ingestion/types.ts@@ -81,10 +81,10 @@ const OpenAICompletionUsageSchema = zcompletion_tokens: z.number().int().nonnegative(),total_tokens: z.number().int().nonnegative(),prompt_tokens_details: z- .record(z.string(), z.number().int().nonnegative())+ .record(z.string(), z.number().int().nonnegative().nullish()).nullish(),completion_tokens_details: z- .record(z.string(), z.number().int().nonnegative())+ .record(z.string(), z.number().int().nonnegative().nullish()).nullish(),}).strict()@@ -110,15 +110,19 @@ const OpenAICompletionUsageSchema = zif (prompt_tokens_details) {for (const [key, value] of Object.entries(prompt_tokens_details)) {- result[`input_${key}`] = value;- result.input = Math.max(result.input - (value ?? 0), 0);+ if (value !== null && value !== undefined) {+ result[`input_${key}`] = value;+ result.input = Math.max(result.input - (value ?? 0), 0);+ }}}if (completion_tokens_details) {for (const [key, value] of Object.entries(completion_tokens_details)) {- result[`output_${key}`] = value;- result.output = Math.max(result.output - (value ?? 0), 0);+ if (value !== null && value !== undefined) {+ result[`output_${key}`] = value;+ result.output = Math.max(result.output - (value ?? 0), 0);+ }}}@@ -133,10 +137,10 @@ const OpenAIResponseUsageSchema = zoutput_tokens: z.number().int().nonnegative(),total_tokens: z.number().int().nonnegative(),input_tokens_details: z- .record(z.string(), z.number().int().nonnegative())+ .record(z.string(), z.number().int().nonnegative().nullish()).nullish(),output_tokens_details: z- .record(z.string(), z.number().int().nonnegative())+ .record(z.string(), z.number().int().nonnegative().nullish()).nullish(),}).strict()@@ -162,15 +166,19 @@ const OpenAIResponseUsageSchema = zif (input_tokens_details) {for (const [key, value] of Object.entries(input_tokens_details)) {- result[`input_${key}`] = value;- result.input = Math.max(result.input - (value ?? 0), 0);+ if (value !== null && value !== undefined) {+ result[`input_${key}`] = value;+ result.input = Math.max(result.input - (value ?? 0), 0);+ }}}if (output_tokens_details) {for (const [key, value] of Object.entries(output_tokens_details)) {- result[`output_${key}`] = value;- result.output = Math.max(result.output - (value ?? 0), 0);+ if (value !== null && value !== undefined) {+ result[`output_${key}`] = value;+ result.output = Math.max(result.output - (value ?? 0), 0);+ }}}diff --git a/web/public/generated/api/openapi.yml b/web/public/generated/api/openapi.ymlindex f7099c7ce0..3ac6b81ed1 100644--- a/web/public/generated/api/openapi.yml+++ b/web/public/generated/api/openapi.yml@@ -4995,11 +4995,13 @@ components:type: objectadditionalProperties:type: integer+ nullable: truenullable: truecompletion_tokens_details:type: objectadditionalProperties:type: integer+ nullable: truenullable: truerequired:- prompt_tokens@@ -5020,11 +5022,13 @@ components:type: objectadditionalProperties:type: integer+ nullable: truenullable: trueoutput_tokens_details:type: objectadditionalProperties:type: integer+ nullable: truenullable: truerequired:- input_tokensdiff --git a/worker/src/services/IngestionService/tests/IngestionService.integration.test.ts b/worker/src/services/IngestionService/tests/IngestionService.integration.test.tsindex 9ea8ae8532..1555c99df5 100644--- a/worker/src/services/IngestionService/tests/IngestionService.integration.test.ts+++ b/worker/src/services/IngestionService/tests/IngestionService.integration.test.ts@@ -346,6 +346,33 @@ describe("Ingestion end-to-end tests", () => {output_reasoning_tokens: 4,},},+ {+ usage: null,+ usageDetails: {+ prompt_tokens: 5,+ completion_tokens: 11,+ total_tokens: 16,+ prompt_tokens_details: {+ cached_tokens: 2,+ audio_tokens: null,+ image_tokens: undefined,+ },+ completion_tokens_details: {+ text_tokens: 3,+ audio_tokens: undefined,+ reasoning_tokens: 4,+ image_tokens: null,+ },+ },+ expectedUsageDetails: {+ input: 3,+ output: 4,+ total: 16,+ input_cached_tokens: 2,+ output_text_tokens: 3,+ output_reasoning_tokens: 4,+ },+ },// OpenAI Response API format{usage: null,@@ -374,6 +401,33 @@ describe("Ingestion end-to-end tests", () => {output_reasoning_tokens: 4,},},+ {+ usage: null,+ usageDetails: {+ input_tokens: 5,+ output_tokens: 11,+ total_tokens: 16,+ input_tokens_details: {+ cached_tokens: 2,+ audio_tokens: null,+ image_tokens: undefined,+ },+ output_tokens_details: {+ text_tokens: 3,+ audio_tokens: null,+ reasoning_tokens: 4,+ image_tokens: undefined,+ },+ },+ expectedUsageDetails: {+ input: 3,+ output: 4,+ total: 16,+ input_cached_tokens: 2,+ output_text_tokens: 3,+ output_reasoning_tokens: 4,+ },+ },].forEach((testConfig) => {it(`should create trace, generation and score without matching models ${JSON.stringify(testConfig,