Skip to content

🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.

fix(v2/observations): declare and normalize enrichment price fields (#13711)

Niklas Semmler612ffdfadab6
diff --git a/fern/apis/server/definition/commons.yml b/fern/apis/server/definition/commons.yml
index 2618781bf5..9d8966fc1c 100644
--- a/fern/apis/server/definition/commons.yml
+++ b/fern/apis/server/definition/commons.yml
@@ -321,10 +321,19 @@ types:
type: optional<nullable<double>>
docs: The time to first token in seconds
- # Enrichment fields (added by server-side enrichment)
+ # Enrichment fields (always present on v2 responses, null when `model` field group not requested)
modelId:
- type: optional<nullable<string>>
- docs: The matched model ID
+ type: nullable<string>
+ docs: The matched model ID. Null when the `model` field group is not requested.
+ inputPrice:
+ type: nullable<string>
+ docs: The input token price (USD per unit) from the matched model, serialized as a decimal string (e.g. "0.0001"). Null when the `model` field group is not requested.
+ outputPrice:
+ type: nullable<string>
+ docs: The output token price (USD per unit) from the matched model, serialized as a decimal string (e.g. "0.0001"). Null when the `model` field group is not requested.
+ totalPrice:
+ type: nullable<string>
+ docs: The total token price (USD per unit) from the matched model, serialized as a decimal string (e.g. "0.0001"). Null when the `model` field group is not requested.
# Trace context fields (field group: trace_context)
traceName:
diff --git a/packages/shared/src/domain/observation-field-groups.ts b/packages/shared/src/domain/observation-field-groups.ts
index a6bbf6164b..5405b992e5 100644
--- a/packages/shared/src/domain/observation-field-groups.ts
+++ b/packages/shared/src/domain/observation-field-groups.ts
@@ -14,6 +14,22 @@
* Defined in the client-safe domain layer (not inside the server-only
* repository file) so frontend forms and Zod enums can reference the values
* without pulling in repository runtime code.
+ *
+ * Enrichment fields (not driven by the field groups above):
+ * - The repository layer adds four enrichment fields to every v2 response row
+ * regardless of the requested `fields`: `modelId`, `inputPrice`,
+ * `outputPrice`, `totalPrice`.
+ * - For the v2 public API, the Postgres lookup that populates them only runs
+ * when `"model"` is in `fields`; otherwise the fields are returned as null.
+ * - For the blob export path, the gate is broader: `"model"` OR `"usage"`
+ * triggers selection of the `model_export` SQL field set so pricing can be
+ * enriched into the usage payload.
+ * - Code references:
+ * - packages/shared/src/server/repositories/events.ts:
+ * `enrichObservationsWithModelData` (v2 read path) and the export
+ * streaming path that gates `model_export`.
+ * - packages/shared/src/server/queries/clickhouse-sql/event-query-builder.ts:
+ * `FIELD_SETS` and `EVENTS_FIELDS` for the underlying column projections.
*/
export const OBSERVATION_FIELD_GROUPS_PUBLIC_API = [
diff --git a/packages/shared/src/server/queries/createGenerationsQuery.ts b/packages/shared/src/server/queries/createGenerationsQuery.ts
index 417bf44b90..da3e7e21f7 100644
--- a/packages/shared/src/server/queries/createGenerationsQuery.ts
+++ b/packages/shared/src/server/queries/createGenerationsQuery.ts
@@ -36,8 +36,9 @@ export type FullEventsObservation = AdditionalObservationFields &
export type FullEventsObservations = Array<FullEventsObservation>;
// Public API version of EventsObservation, some fields are omitted because
-// V2 allows clients to specify fields
+// V2 allows clients to specify fields.
+// modelId is always added by enrichObservationsWithModelData (null when model group not requested).
export type EventsObservationPublic = Partial<
EventsObservation & ObservationPriceFields
> &
- ObservationCoreFields;
+ ObservationCoreFields & { modelId: string | null };
diff --git a/web/public/generated/api/openapi.yml b/web/public/generated/api/openapi.yml
index 24f52fd090..37be534ed6 100644
--- a/web/public/generated/api/openapi.yml
+++ b/web/public/generated/api/openapi.yml
@@ -8126,7 +8126,30 @@ components:
modelId:
type: string
nullable: true
- description: The matched model ID
+ description: >-
+ The matched model ID. Null when the `model` field group is not
+ requested.
+ inputPrice:
+ type: string
+ nullable: true
+ description: >-
+ The input token price (USD per unit) from the matched model,
+ serialized as a decimal string (e.g. "0.0001"). Null when the
+ `model` field group is not requested.
+ outputPrice:
+ type: string
+ nullable: true
+ description: >-
+ The output token price (USD per unit) from the matched model,
+ serialized as a decimal string (e.g. "0.0001"). Null when the
+ `model` field group is not requested.
+ totalPrice:
+ type: string
+ nullable: true
+ description: >-
+ The total token price (USD per unit) from the matched model,
+ serialized as a decimal string (e.g. "0.0001"). Null when the
+ `model` field group is not requested.
traceName:
type: string
nullable: true
diff --git a/web/src/features/public-api/types/observations.ts b/web/src/features/public-api/types/observations.ts
index 72c257e212..23f60c56a2 100644
--- a/web/src/features/public-api/types/observations.ts
+++ b/web/src/features/public-api/types/observations.ts
@@ -441,8 +441,14 @@ const APIObservationV2 = z
latency: z.number().nullable().optional(),
timeToFirstToken: z.number().nullable().optional(),
- // Enrichment fields
- modelId: z.string().nullable().optional(),
+ // Enrichment fields (always present on v2 responses).
+ // Populated only when "model" is in the `fields` query param; otherwise null.
+ // Prices are strings (serialized from Prisma Decimal) to preserve backward compatibility
+ // with callers who built typed schemas against the initial v2 wire format.
+ modelId: z.string().nullable(),
+ inputPrice: z.string().nullable(),
+ outputPrice: z.string().nullable(),
+ totalPrice: z.string().nullable(),
// Trace context fields (field group: trace_context)
traceName: z.string().nullable().optional(),
diff --git a/web/src/pages/api/public/v2/observations/index.ts b/web/src/pages/api/public/v2/observations/index.ts
index 4fb5bf8bd4..e100cfa288 100644
--- a/web/src/pages/api/public/v2/observations/index.ts
+++ b/web/src/pages/api/public/v2/observations/index.ts
@@ -57,13 +57,18 @@ export default withMiddlewares({
const hasMore = items.length > query.limit;
const dataToReturn = hasMore ? items.slice(0, query.limit) : items;
- // Convert empty parent_observation_id to null for consistency with v1
- const transformedItems = dataToReturn.map((item) => {
- if (item.parentObservationId === "") {
- return { ...item, parentObservationId: null };
- }
- return item;
- });
+ // Normalize for the wire format:
+ // - empty parent_observation_id -> null (v1 parity)
+ // - Decimal price fields -> string (preserves original v2 wire format; v1 emits numbers via .toNumber())
+ const transformedItems = dataToReturn.map((item) => ({
+ ...item,
+ parentObservationId:
+ item.parentObservationId === "" ? null : item.parentObservationId,
+ modelId: item.modelId ?? null,
+ inputPrice: item.inputPrice?.toString() ?? null,
+ outputPrice: item.outputPrice?.toString() ?? null,
+ totalPrice: item.totalPrice?.toString() ?? null,
+ }));
// Generate cursor if there are more results
const lastItemIdx = dataToReturn.length - 1;