πͺ’ 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
feat(io-normalization): define normalized I/O contract and projections (#16216)
marliessophie5644bac4e62f
diff --git a/packages/shared/AGENTS.md b/packages/shared/AGENTS.mdindex 088d766a47..13ed785621 100644--- a/packages/shared/AGENTS.md+++ b/packages/shared/AGENTS.md@@ -73,7 +73,9 @@`@langfuse/shared/src/server/auth/apiKeys`,`@langfuse/shared/src/server/ee/ingestionMasking`,`@langfuse/shared/src/server/llm/llmText`, and- `@langfuse/shared/src/utils/chatml`.+ `@langfuse/shared/src/utils/chatml`. The experimental+ `@langfuse/shared/src/utils/normalized-io` parser is client-safe but **do not+ use it yet**; its public contract is still being validated.When changing export surfaces, keep `package.json#exports`, the relevant barrelfile (`src/index.ts`, `src/server/index.ts`, etc.), and this guide aligned indiff --git a/packages/shared/package.json b/packages/shared/package.jsonindex 3829811350..d28ce20a24 100644--- a/packages/shared/package.json+++ b/packages/shared/package.json@@ -47,6 +47,10 @@"import": "./dist/src/utils/chatml/index.js","require": "./dist/src/utils/chatml/index.js"},+ "./src/utils/normalized-io": {+ "import": "./dist/src/utils/normalized-io/index.js",+ "require": "./dist/src/utils/normalized-io/index.js"+ },"./encryption": {"import": "./dist/src/encryption/index.js","require": "./dist/src/encryption/index.js"diff --git a/packages/shared/src/server/index.ts b/packages/shared/src/server/index.tsindex 98861f5be4..b2595c3464 100644--- a/packages/shared/src/server/index.ts+++ b/packages/shared/src/server/index.ts@@ -85,6 +85,7 @@ export * from "./ingestion/ingestionAttribution";export * from "./ingestion/processEventBatch";export * from "../server/ingestion/validateAndInflateScore";export * from "./ingestion/extractToolsBackend";+export * from "./normalized-io";export * from "../server/queries/public-api-filter-builder";export * from "../server/pricing-tiers";export * from "./redis/redis";diff --git a/packages/shared/src/server/normalized-io/index.ts b/packages/shared/src/server/normalized-io/index.tsnew file mode 100644index 0000000000..b728952701--- /dev/null+++ b/packages/shared/src/server/normalized-io/index.ts@@ -0,0 +1,2 @@+export * from "./parser";+export * from "./projections";diff --git a/packages/shared/src/server/normalized-io/parser.ts b/packages/shared/src/server/normalized-io/parser.tsnew file mode 100644index 0000000000..e2ce653471--- /dev/null+++ b/packages/shared/src/server/normalized-io/parser.ts@@ -0,0 +1,73 @@+import type { EventRecordBaseType } from "../repositories/definitions";+import { metadataArraysToRecord } from "../utils/metadata_conversion";+import type { ResourceSpan } from "../otel/OtelIngestionProcessor";+import { normalizeSpanIO } from "../../utils/normalized-io";+import type { NormalizedIO, SpanIO } from "../../utils/normalized-io";++/**+ * Orchestration only: adapts a source into `SpanIO`, decodes exactly one+ * JSON-string boundary per value, and wires the container/message/part+ * discovery in `core/*.ts` together. Zero provider-specific vocabulary+ * lives in this file β every provider dialect is a fold over+ * `registeredProviders()` inside `core/*.ts`.+ */++type OtelScopeSpan = NonNullable<ResourceSpan["scopeSpans"]>[number];+export type OtelSpan = NonNullable<OtelScopeSpan["spans"]>[number];+export type OtelScope = OtelScopeSpan["scope"];++export type OtelSpanContext = {+ // The full instrumentation scope: name drives format detection, and+ // name/version/attributes all flow into SpanIO metadata.+ scope: OtelScope;+ resourceAttributes: Record<string, unknown>;+};++type EventRecordIOColumns = Pick<+ EventRecordBaseType,+ "input" | "output" | "metadata_names" | "metadata_values"+>;++export type NormalizeIOSource =+ | { kind: "event-record"; record: EventRecordIOColumns }+ | { kind: "io"; io: SpanIO }+ | { kind: "otel"; span: OtelSpan; context: OtelSpanContext };++/**+ * OTel span -> SpanIO, one span (= one observation) at a time.+ *+ * Framework-specific extraction is intentionally still a follow-up. The+ * ingestion processor remains the source of truth until that logic is moved+ * behind this private helper.+ */+function spanIOFromOtelSpan(_span: OtelSpan, _ctx: OtelSpanContext): SpanIO {+ throw new Error("spanIOFromOtelSpan is not implemented yet");+}++function toSpanIO(source: NormalizeIOSource): SpanIO {+ switch (source.kind) {+ case "event-record":+ return {+ input: source.record.input ?? null,+ output: source.record.output ?? null,+ metadata:+ metadataArraysToRecord(+ source.record.metadata_names,+ source.record.metadata_values,+ ) ?? null,+ };+ case "io":+ return source.io;+ case "otel":+ return spanIOFromOtelSpan(source.span, source.context);+ }+}++// ---------------------------------------------------------------------------+// Entry point+// ---------------------------------------------------------------------------++export function normalizeIO(source: NormalizeIOSource): NormalizedIO {+ const span = toSpanIO(source);+ return normalizeSpanIO(span);+}diff --git a/packages/shared/src/server/normalized-io/projections/eval-record.ts b/packages/shared/src/server/normalized-io/projections/eval-record.tsnew file mode 100644index 0000000000..0e4fecc880--- /dev/null+++ b/packages/shared/src/server/normalized-io/projections/eval-record.ts@@ -0,0 +1,92 @@+import type {+ NormalizedIO,+ NormalizedMessage,+ ToolDefinition,+} from "../../../utils/normalized-io/types";+import {+ ObservationForEval,+ ToolCallForEval,+} from "../../../features/evals/observationForEval";++/**+ * The 6 fields on ObservationForEval that the parser actually computes.+ * Everything else (identifiers, usage/cost, experiment fields, ...) is+ * unrelated to message/tool-call parsing and passes through from the+ * ClickHouse record unchanged.+ *+ * TODO: shape still being tightened; not consumed by any production path yet.+ */+export type NormalizedEvalRecord = Omit<+ ObservationForEval,+ | "input"+ | "output"+ | "tool_definitions"+ | "tool_calls"+ | "tool_call_names"+ | "tool_call_count"+> & {+ input: NormalizedMessage[];+ output: NormalizedMessage[];+ toolCalls: ToolCallForEval[];+ toolDefinitions: ToolDefinition[];+};++/**+ * (NormalizedIO, ObservationForEval) -> eval-ready record. A spread-and-+ * override rather than an enumeration of all of ObservationForEval's+ * fields, so this projection does not need updating when ObservationForEval+ * gains fields unrelated to I/O parsing.+ */+export function toEvalRecord(+ io: NormalizedIO,+ record: ObservationForEval,+): NormalizedEvalRecord {+ const {+ input: _rawInput,+ output: _rawOutput,+ tool_definitions: _rawToolDefinitions,+ tool_calls: _rawToolCalls,+ tool_call_names: _rawToolCallNames,+ tool_call_count: _rawToolCallCount,+ ...passthrough+ } = record;+ const toolCalls: ToolCallForEval[] = [];+ const input: NormalizedMessage[] = [];+ const output: NormalizedMessage[] = [];++ for (const message of io.messages) {+ if (message.source === "input") {+ input.push(message);+ } else {+ output.push(message);+ }++ // Calls only ever come from the output side: a tool-call part on an+ // input-tagged message is history from an earlier turn, not something+ // this observation newly called+ if (message.source !== "output") continue;++ // Parallel-call slot within the message (chat-completions `index`+ // semantics: tool_calls[i].index === i in assembled payloads).+ let callIndex = 0;+ for (const part of message.parts) {+ if (part.type !== "tool-call") continue;++ toolCalls.push({+ id: part.toolCallId ?? "",+ name: part.toolName,+ arguments: part.input ?? {},+ type: part.toolType ?? "",+ index: callIndex++,+ });+ }+ }++ return {+ ...passthrough,+ input,+ output,+ toolCalls,+ toolDefinitions: io.toolDefinitions,+ };+}diff --git a/packages/shared/src/server/normalized-io/projections/index.ts b/packages/shared/src/server/normalized-io/projections/index.tsnew file mode 100644index 0000000000..16a577dd88--- /dev/null+++ b/packages/shared/src/server/normalized-io/projections/index.ts@@ -0,0 +1,2 @@+export * from "./eval-record";+export * from "./tool-call-columns";diff --git a/packages/shared/src/server/normalized-io/projections/tool-call-columns.ts b/packages/shared/src/server/normalized-io/projections/tool-call-columns.tsnew file mode 100644index 0000000000..23f95139c9--- /dev/null+++ b/packages/shared/src/server/normalized-io/projections/tool-call-columns.ts@@ -0,0 +1,58 @@+import type {+ NormalizedIO,+ ToolColumns,+} from "../../../utils/normalized-io/types";++/**+ * NormalizedIO -> ClickHouse tool-call columns. Shape matches+ * extractToolsBackend.ts's convertCallsToArrays/convertDefinitionsToMap+ * exactly, since this projection is meant to eventually replace that path.+ * Output-side tool calls have already been deduplicated by the parser.+ */+export function toToolColumns(io: NormalizedIO): ToolColumns {+ const tool_calls: string[] = [];+ const tool_call_names: string[] = [];++ // Calls only ever come from the output side: a tool-call part on an+ // input-tagged message is history from an earlier turn (already resolved),+ // not something this observation newly called+ for (const message of io.messages) {+ if (message.source !== "output") continue;++ // Parallel-call slot within the message (chat-completions `index`+ // semantics: tool_calls[i].index === i in assembled payloads). Counted+ // before the invalid filter so emitted slots match the raw payload.+ let callIndex = 0;+ for (const part of message.parts) {+ if (part.type !== "tool-call") continue;+ const index = callIndex++;+ // Columns count executable calls only; attempts whose arguments could+ // not be parsed stay out β legacy parity, the legacy extractor never+ // saw unparsed calls.+ if (part.invalid === true) continue;++ tool_call_names.push(part.toolName);+ tool_calls.push(+ JSON.stringify({+ id: part.toolCallId ?? "",+ arguments: JSON.stringify(part.input ?? {}),+ type: part.toolType ?? "",+ index,+ }),+ );+ }+ }++ const tool_definitions: Record<string, string> = {};+ for (const definition of io.toolDefinitions) {+ tool_definitions[definition.name] = JSON.stringify({+ description: definition.description ?? "",+ parameters:+ definition.inputSchema !== undefined+ ? JSON.stringify(definition.inputSchema)+ : "",+ });+ }++ return { tool_definitions, tool_calls, tool_call_names };+}diff --git a/packages/shared/src/utils/IORepresentation/chatML/types.ts b/packages/shared/src/utils/IORepresentation/chatML/types.tsindex 6176682bf0..cbf76837e1 100644--- a/packages/shared/src/utils/IORepresentation/chatML/types.ts+++ b/packages/shared/src/utils/IORepresentation/chatML/types.ts@@ -36,6 +36,7 @@ export type ParsedMediaReferenceType = z.infer<typeof ParsedMediaReferenceSchema>;+// TODO: move to parser// Matches the format parsed by MediaReferenceStringSchema. Non-greedy, so// consecutive references in one string are separate matches.export const MEDIA_REFERENCE_PATTERN = /@@@langfuseMedia:.+?@@@/g;@@ -47,6 +48,7 @@ export const MEDIA_REFERENCE_PATTERN = /@@@langfuseMedia:.+?@@@/g;* Note: This schema uses transforms that can throw errors during validation* if the magic string format is invalid. Always use with try-catch or safeParse.*/+// TODO: move to parserexport const MediaReferenceStringSchema = z.string().transform((str, ctx) => {diff --git a/packages/shared/src/utils/normalized-io/README.md b/packages/shared/src/utils/normalized-io/README.mdnew file mode 100644index 0000000000..736b370336--- /dev/null+++ b/packages/shared/src/utils/normalized-io/README.md@@ -0,0 +1,234 @@+# Normalized I/O parser++One parser owns observation I/O interpretation. `normalizeIO(source)` turns+raw observation input/output/metadata into a canonical, provider-independent+representation; downstream consumers (ClickHouse tool columns, eval records,+rendering) derive their shapes from that representation.++## TL;DR++Please only use the normalized parser behind feature flags at this time.+It has not been sufficiently validated against production data yet.++A telemetry JSON blob can contain the same conversation under keys such as+`messages`, `choices`, or `contents`. The parser handles input and output+separately and does three things:++1. **Claim the messages once.** Providers are checked in the explicit order in+ `conventions/index.ts`. The first provider that recognizes a conversation+ container wins. If none does, the parser uses `messages`, then the value+ itself. Other message containers are ignored, so the conversation is not+ added twice.+2. **Normalize those messages.** Each message is split into parts such as text,+ tool calls, tool results, and files. The provider that claimed the messages+ is tried first for each part, but all other providers remain fallbacks. A+ top-level system instruction is added only when the messages do not already+ contain a system message.+3. **Collect tool definitions separately.** Claiming one message container does+ not hide tools stored under another known key or in metadata.++Known fields become the normalized shape. Anything else is kept in+`providerMetadata`, so provider-specific information is not silently lost.++## Interface++```ts+normalizeIO(source: NormalizeIOSource): NormalizedIO++type NormalizeIOSource =+ | { kind: "event-record"; record } // ClickHouse event row (metadata as name/value column arrays)+ | { kind: "io"; io } // { input, output, metadata }+ | { kind: "otel"; span; context }; // raw OTel span+```++`NormalizedIO` contains:++- `messages` β one ordered stream: input messages, then output messages,+ original order preserved within each. Every message carries `role`,+ ordered `parts`, an optional `finishReason`, and `source: "input" | "output"`+ (the observation boundary, recoverable by projections).+- `toolDefinitions` β the tools available to the model, merged across+ input, output, and metadata.+- `span` β the raw pre-normalization `{ input, output, metadata }`, always+ returned.++## The part union++Parts encode _what a consumer can do with the content_. Once known fields are+normalized, every unconsumed provider field is preserved in part-level+`providerMetadata`, never as an extra part type. Tool definitions follow the+same remainder rule. Entirely unknown typed blocks remain lossless `custom`+parts, while untyped structured values remain lossless `data` parts.++| Part | Content | Notes |+| ------------- | --------------------------------------------------------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |+| `text` | `text` | citations/annotations land in `providerMetadata.citations` |+| `reasoning` | `content` union: `{kind:"text", text, signature?}` \| `{kind:"redacted", data}` \| `{kind:"encrypted", data}` \| `{kind:"data", value}` | visible CoT/summaries; provider-withheld blobs; replayable encrypted blobs; structured payloads |+| `tool-call` | `toolCallId` (nullable), `toolName`, `input`, `toolType?`, `providerExecuted?` | `toolType` preserves the source kind (`function`, `custom`, provider-specific built-ins); `providerExecuted` marks server-side tools; parallel-call slot indexes are derived positionally by projections, raw streaming `index` fields stay in `providerMetadata` |+| `tool-result` | `toolCallId` (nullable), `toolName?`, `output`, `isError?` | AI-SDK `{type, value}` output wrappers are unwrapped |+| `file` | `content` union: `{kind:"url"}` \| `{kind:"base64"}` \| `{kind:"reference", id}`, `mediaType?`, `filename?` | all media, incl. `@@@langfuseMedia:β¦@@@` tokens β references; `mediaType` is exact when declared, a modality wildcard (`image/*`) when only the part kind reveals it, absent for opaque ids |+| `data` | `value` | structured non-message payloads (function-span args/results, loose records) β JSON-passthrough parity with the trace view |+| `custom` | `kind`, `value` | recognized-but-unmapped typed blocks, verbatim (e.g. `source`, `document`) |++Parser-computed semantics are typed fields on their parts:+`TextPart.refusal` (model refusals stay findable text), `FilePart.reasoning`+(reasoning-generated files), `ToolCallPart.invalid` (unparsable tool-call+attempts β visible in the stream, excluded from tool columns).+`providerMetadata` carries the verbatim provider-field remainder plus+parser-derived provider payloads (citations, transcripts, server labels,+statuses). Canonical fields and provider metadata do not duplicate the same+raw field. A provider concept graduates to a typed field once it is a+cross-provider semantic we compute and consumers filter on.++`finishReason` is `{ type, raw }`: provider vocabularies (OpenAI+`finish_reason`, Anthropic `stop_reason`, Gemini `finishReason`, AI-SDK+variants) canonicalize to+`stop | length | tool-calls | content-filter | error | other | unknown`,+with the provider's verbatim value kept in `raw`.++## How parsing works++Recognition is **shape-based** β the parser never dispatches on a provider+label, because real payloads are mixed-dialect (AI-SDK and Anthropic blocks+in one message, LangChain wrapping OpenAI). Provider vocabulary lives in+per-provider `IOConvention` objects under `conventions/providers/<name>/`+(typed part handlers, envelope unwraps, root containers, tool-definition+recognizers, role/finish-reason maps); `core/` holds the generic pipeline+and folds over the registry in `conventions/index.ts`, ordered common+providers first. Adding a provider is a directory plus one registry entry β+core never changes. The pipeline:++1. **Source β raw values.** Event rows zip their metadata name/value column+ arrays; plain IO passes through; OTel extraction is a stub pending the+ processor extraction. Every value crosses **at most+ one JSON-string boundary per owner** β nested encoded strings are decoded+ by whichever step consumes them, never eagerly (double-encoded payloads+ are common; eager deep-parsing corrupts legitimate strings).+2. **Container discovery.** Top-level shapes are unwrapped: `messages`+ arrays, bare item arrays (OpenAI Responses style β standalone tool-call+ items batch into synthetic assistant messages; `mcp_list_tools` feeds+ `toolDefinitions` side-band), `choices[]` / `candidates[]` response+ envelopes (choice-level finish reasons attach to their message), Gemini+ `contents` + `system_instruction`, or a single record.+3. **Message normalization.** Envelope unwraps first (Semantic Kernel+ `gen_ai.event.content`, the LangChain `lc`/`kwargs` serialization+ envelope with the role derived from the class path, GenAI choice events+ `{index, message, finish_reason}`), then Python-repr message strings+ (agno). Roles normalize to+ `system | user | assistant | tool` with deliberate+ coercions: `model β assistant`, deprecated `function β tool` (name becomes+ the tool name), role-less Responses reasoning items β `assistant` (model+ output even in replayed input history), user messages consisting solely of+ tool results β `tool`, tool-labeled turns without a `tool_call_id` β+ assistant call batches or tool-results (the content _is_ the tool's+ response), unrecognized declared roles (multi-agent frameworks putting the+ agent name in the role field) β the contextual fallback role with the raw+ string preserved as `senderName`. `senderName` otherwise carries an+ explicit participant `name` (OpenAI/LangChain).+4. **Part normalization.** Per message: content arrays/parts run through the+ part parser; string content splits into interleaved text and file parts+ around embedded media tokens; JSON-string arrays of tool shapes parse into+ tool parts. Sibling fields fold in: `tool_calls` (++ `additional_kwargs.tool_calls`), `invalid_tool_calls` (flagged), Anthropic+ `thinking` arrays, OpenAI `refusal`/`audio`/`annotations`, Responses+ reasoning `summary`/`encrypted_content`, `finish_reason` (message-level,+ envelope-level, or `response_metadata`). The part parser itself+ recognizes, in order: shared typed blocks (text, reasoning), the+ provider-declared typed blocks (OpenAI, Anthropic, AI-SDK block types,+ including provider-executed built-in items), untyped keyed parts (Gemini+ `functionCall`, `inlineData`, `executableCode`, bare `{text}` with+ `thought`/`thoughtSignature`), shape-sniffed tool calls without a+ recognized type, and finally the `data`/`custom` fallback. Messages that produce no parts+ but have no message keys become single `data`-part messages (JSON+ passthrough) rather than being dropped.+5. **Accumulation.** Within one message, the first carrier of a tool call wins,+ so the same call in both `content` and `tool_calls` is not added twice.+ Repeated calls in input history remain. Output deduplicates calls by name and arguments. A call present in both input and output remains+ on both sides. Tool definitions merge by name with first-seen-wins per field+ across input β output β metadata.++## Semantics and invariants++Each entry states the behavior, why, and how to challenge it. Challenge by+fixture: add a failing case to the test suite, then either the behavior falls+or the fixture documents it.++### Tool calls++- **Duplicate carriers are not duplicate calls.** If one message exposes the+ same call in `content` and `tool_calls`, the first carrier wins. Identical+ calls within one carrier or in different input messages remain valid calls.+- **Output dedup remains output-only.** It uses `toolCallId`, falling+ back to the raw name + arguments. Input history is not globally deduplicated,+ and the same call may survive once in input and once in output.+- **Absent ids are `null`;** the column projection maps `null` to `""` for+ byte-compatibility with the legacy ClickHouse format.+- **Tool columns count executable output-side calls only**: input-side calls+ are history from earlier turns; `invalid`-flagged attempts are excluded.+ Consumers that need history calls read `messages`, not the columns.+- **Unnamed provider tools are admitted** into `toolDefinitions` (name+ derived from the tool type, e.g. `web_search_preview`) β available-tool+ filtering covers provider built-ins; the legacy extractor's exclusion was+ accidental.++### Messages and content++- **`source` preserves the input/output boundary in one ordered stream** β+ rendering wants one conversation; projections need the boundary back.+- **No provider label on the result.** One span routinely mixes dialects, so+ an observation-level label would either echo span metadata (redundant β+ consumers already have `gen_ai.system`/`ls_provider`/model columns) or make+ a wrong claim about mixed content. Labels come from span metadata; per-part+ dialect traces exist via `toolType` and providerMetadata keys;+ round-tripping uses the raw `span`.+- **`tool` stays a role.** Turns carry the protocol rhythm; the `tool-result`+ part carries the semantics β tool messages are not collapsed into the+ preceding assistant turn.+- **Messages whose only content is an empty string are dropped.** Non-string+ payloads are never coerced to text β structured values become `data`.+- **Media normalizes to file parts.** Reference tokens (the dominant stored+ shape after ingestion uploads payloads) map to `{kind: "reference", id}`+ with `mediaType` from the token β exactly what `LangfuseMediaView`+ resolves. Raw base64 data-URIs (only present when upstream media+ processing skipped or failed) stay `url` content; the parser reads the+ declared prefix type but never decodes payloads β that is+ `MediaPayloadProcessor`'s job. Text with embedded media tokens splits into+ interleaved text/file parts so text consumers never see tokens.+- **Citations are unified**: Anthropic `citations` and OpenAI `annotations`+ (part- and message-level) land under `providerMetadata.citations`,+ payloads verbatim. Anchor-less references (eg AI-SDK `source` parts) stay+ stream-positioned as `custom {kind: "source"}` β one vocabulary, two+ carriers, matching what the source actually provides.+- **`SpanIO.metadata` may carry an `attributes` record with OTel keys**;+ known tool-definition attribute keys are mined from it, each by the+ provider convention that owns it (`ai.prompt.tools` β AI SDK,+ `gen_ai.tool.definitions` / `llm.tools.N.β¦` β GenAI,+ `model_request_parameters.function_tools` β Pydantic AI).+- **Role** Unrecognized declared roles fall back to the contextual role (`user`+ on` input, assistant on output) with the raw string preserved as senderName.+ The parser does not correlate role strings with known tool names, so such+ turns surface as regular messages, not tool turns.++### Intentional losses++- LangChain `tool_call_chunks` (streaming deltas, redundant with parsed+ `tool_calls` on final messages).+- Anthropic text/structured-content documents become+ `custom {kind: "document"}` β semantic content, not resolvable files.++## Format coverage++OpenAI Chat Completions + Responses API, Anthropic Messages, Vercel AI SDK,+Gemini/Vertex, LangChain/LangGraph (incl. the `lc` serialization envelope),+Microsoft Agent Framework, Pydantic AI, Semantic Kernel, agno/koog-style+loose shapes, GenAI event streams, and raw passthrough. Providers without+dedicated handling (Bedrock, Mistral, Cohere, β¦) ride the generic+shape-sniffing paths; targeted handling is added only when validation+produces a concrete miss.++## Projections++- `projections/tool-call-columns.ts` β `NormalizedIO` β the legacy ClickHouse+ `tool_definitions`/`tool_calls`/`tool_call_names` column format.+- `projections/eval-record.ts` β `NormalizedIO` + observation β eval-ready+ record (input/output message split, output-side tool calls).diff --git a/packages/shared/src/utils/normalized-io/conventions/index.ts b/packages/shared/src/utils/normalized-io/conventions/index.tsnew file mode 100644index 0000000000..eea44fc4fc--- /dev/null+++ b/packages/shared/src/utils/normalized-io/conventions/index.ts@@ -0,0 +1,41 @@+import type { IOConvention } from "./io-convention";+import { agnoProvider } from "./providers/agno";+import { aiSdkProvider } from "./providers/ai-sdk";+import { anthropicProvider } from "./providers/anthropic";+import { geminiProvider } from "./providers/gemini";+import { langchainProvider } from "./providers/langchain";+import { openAiProvider } from "./providers/openai";+import { otelGenaiProvider } from "./providers/otel-genai";+import { pydanticAiProvider } from "./providers/pydantic-ai";+import { semanticKernelProvider } from "./providers/semantic-kernel";++export type { IOConvention } from "./io-convention";+export { claimed, dropped, unmatched } from "./io-convention";++/**+ * All registered provider conventions, in deliberate order: common providers+ * first, so every fold checks the hot dialects before the long tail.+ * Part and tool-definition shapes are mostly disjoint, but message claims are+ * intentionally exclusive: the first provider claiming a conversation owns+ * that carrier. Keep this list ordered from the most specific/common root+ * envelopes to the generic tail; parser context then tries the selected+ * provider first while normalizing nested parts.+ * Adding a provider is a directory plus one entry here; `registry.test.ts`+ * asserts folder <-> registry parity.+ *+ * Eager: provider files depend on canonical part builders and JSON helpers,+ * never on `normalize/message.ts`, `normalize/part.ts`, or the accumulator+ * collector, which import this registry, so there is no import cycle to+ * protect against.+ */+export const registeredProviders: readonly IOConvention[] = [+ openAiProvider,+ anthropicProvider,+ geminiProvider,+ langchainProvider,+ aiSdkProvider,+ otelGenaiProvider,+ pydanticAiProvider,+ semanticKernelProvider,+ agnoProvider,+];diff --git a/packages/shared/src/utils/normalized-io/conventions/io-convention.ts b/packages/shared/src/utils/normalized-io/conventions/io-convention.tsnew file mode 100644index 0000000000..4110e38780--- /dev/null+++ b/packages/shared/src/utils/normalized-io/conventions/io-convention.ts@@ -0,0 +1,190 @@+import type {+ FinishReason,+ NormalizedMessage,+ NormalizedMessagePart,+ NormalizedMessageRole,+ ToolDefinition,+} from "../types";++/** Exclusive-hook result. `try*` hooks are exclusive: the first provider+ * that returns `matched: true` wins. `value: null` = recognized but+ * intentionally omitted (dropped). */+export type ConventionResult<T> =+ | { matched: false }+ | { matched: true; value: T | null };++export const unmatched = { matched: false } as const;+export const claimed = <T>(value: T | null): ConventionResult<T> => ({+ matched: true,+ value,+});+export const dropped = { matched: true, value: null } as const;++export type PartHandlerContext = {+ normalizePart(value: unknown): NormalizedMessagePart | null;+ normalizePartList(values: unknown[]): NormalizedMessagePart[];+};++export type MessageEnvelopeContext = {+ /** The observation side being normalized β for implementations that+ * construct a message directly instead of recursing. */+ readonly source: "input" | "output";+ /** `source` is closed over by the core β providers cannot mis-thread it. */+ normalizeMessage(+ value: unknown,+ fallbackRole: "user" | "assistant",+ ): NormalizedMessage | null;+ isMessageLike(value: Record<string, unknown>): boolean;+ normalizeFinishReason(+ data: Record<string, unknown>,+ responseMetadata?: Record<string, unknown>,+ ): FinishReason | undefined;+};++export type SiblingPartSlot =+ | "before-content"+ | "after-content"+ | "after-tool-calls";++export type SiblingPartContribution = {+ sourceKey: string; // the field the parts came from, e.g. "thinking"+ slot: SiblingPartSlot;+ parts: NormalizedMessagePart[];+};++type MessageSourceBase = {+ fallbackRole: "user" | "assistant";+};++export type MessageSource =+ | (MessageSourceBase & {+ kind: "single";+ value: unknown;+ roleOverride?: "system";+ /** The record carrying a choice/candidate-level finish reason (the core+ * reads it via `normalizeFinishReason`'s key chain). */+ finishReasonCarrier?: Record<string, unknown>;+ })+ | (MessageSourceBase & {+ kind: "sequence";+ values: unknown[];+ });++export type ToolDefinitionOptions = {+ allowProviderToolWithoutName?: boolean;+ allowToolMap?: boolean;+};++export type ToolDefinitionSource = {+ sourceKey: string;+ value: unknown;+ options?: ToolDefinitionOptions;+};++export type ToolDefinitionCarrier = {+ root?: Record<string, unknown>;+ metadataAttributes?: Record<string, unknown>;+};++export type PartHandler = (+ value: Record<string, unknown>,+ context: PartHandlerContext,+) => ConventionResult<NormalizedMessagePart>;++export interface IOConvention {+ readonly name: string;++ /** Provider finish/stop vocabulary -> canonical set. Lookups lowercased.+ * Overlapping raw values across providers must map identically. */+ readonly finishReasonTypeByRaw?: Readonly<+ Record<string, FinishReason["type"]>+ >;++ /** Lowercased role/author string -> canonical role (gemini `model`,+ * openai deprecated `function`). Same agreement invariant. */+ readonly roleByRawRole?: Readonly<Record<string, NormalizedMessageRole>>;++ /** message.type -> canonical role, used when role/author is absent+ * (langchain `human`/`ai`/`tool`/`system`, openai Responses `reasoning`+ * items which are model output even replayed on the input side). */+ readonly roleByMessageType?: Readonly<Record<string, NormalizedMessageRole>>;++ /** Keys beside the universal `role`/`content` whose presence marks a+ * record as a message container in this dialect (gemini `parts`,+ * langchain `additional_kwargs`, ...). */+ readonly messageLikeKeys?: ReadonlySet<string>;++ /** Keys under which this dialect carries citation payloads (anthropic+ * `citations`, openai `annotations`). The core lifts the first non-empty+ * carrier verbatim into `providerMetadata.citations`. */+ readonly citationKeys?: ReadonlySet<string>;++ /** Messages that are tool *definitions*, not conversation content+ * (koog/Traceloop role-"tool" turns wrapping an OpenAI function tool). */+ isToolDefinitionMessage?(value: Record<string, unknown>): boolean;++ /** Provider-specific raw `type` values ONLY. Shared/contested names+ * (text, image, file, reasoning group) live in normalize/part.ts's+ * SHARED_TYPED_PART_HANDLERS, never here. */+ readonly typedParts?: Readonly<Record<string, PartHandler>>;++ /** Keyed unions without a `type` field (Gemini). */+ tryNormalizeUntypedPart?(+ value: Record<string, unknown>,+ context: PartHandlerContext,+ ): ConventionResult<NormalizedMessagePart>;++ /** Provider-specific string message representations (agno Python reprs). */+ tryPreprocessMessage?(value: string): ConventionResult<unknown>;++ /** Provider-specific message envelopes (langchain lc/kwargs, SK+ * gen_ai.event.content, GenAI choice events). Shapes must stay disjoint+ * across providers. */+ tryUnwrapMessage?(+ value: Record<string, unknown>,+ fallbackRole: "user" | "assistant",+ context: MessageEnvelopeContext,+ ): ConventionResult<NormalizedMessage>;++ /** Parts stored beside the main content field. Cumulative across+ * providers; ordering is by slot, not registry order. */+ collectSiblingParts?(+ value: Record<string, unknown>,+ baseParts: readonly NormalizedMessagePart[],+ context: PartHandlerContext,+ ): SiblingPartContribution[];++ /** Provider-specific message containers at the IO root. The first provider+ * returning a non-empty result claims the conversation. Providers must guard+ * weak keys (for example `output`) so unrelated records do not claim it. */+ claimMessages?(+ root: Record<string, unknown>,+ kind: "input" | "output",+ ): MessageSource[];++ /** Additive system-instruction carrier. It never claims the conversation and+ * is used only when the input messages contain no system message. */+ getSystemMessage?(+ root: Record<string, unknown>,+ kind: "input" | "output",+ ): MessageSource | undefined;++ /** Provider-specific tool-definition carriers (IO root and/or OTel+ * metadata attributes β implementations read the carrier field they own). */+ collectToolDefinitionSources?(+ carrier: ToolDefinitionCarrier,+ ): ToolDefinitionSource[];++ /** Tool-definition ITEM recognizer for this dialect's declaration shapes+ * (openai `function`/`custom` wrappers and flat `format`, anthropic+ * `input_schema`, pydantic-ai `parameters_json_schema`, ai-sdk/MCP+ * `inputSchema`). Items arrive through generic carriers (`root.tools`+ * holds every dialect), so recognition is item-level and cumulative like+ * typed parts. The core's loose fallback handles bare+ * `{name, description, parameters}` items and nameless provider+ * built-ins. Prefer `unmatched` over `claimed(null)` for unconstructible+ * items so the fallback's nameless allowance can still rescue them. */+ tryNormalizeToolDefinition?(+ value: Record<string, unknown>,+ ): ConventionResult<ToolDefinition>;+}diff --git a/packages/shared/src/utils/normalized-io/conventions/providers/agno/agno.test.ts b/packages/shared/src/utils/normalized-io/conventions/providers/agno/agno.test.tsnew file mode 100644index 0000000000..d3a2cad795--- /dev/null+++ b/packages/shared/src/utils/normalized-io/conventions/providers/agno/agno.test.ts@@ -0,0 +1,13 @@+import { describe, expect, it } from "vitest";++import { normalizeSpanIO } from "../../../parser";+import { agnoPythonReprFixture } from "./fixtures";++describe("Agno normalized I/O", () => {+ it.each([agnoPythonReprFixture])("$name", ({ spanIO, expected }) => {+ expect(normalizeSpanIO(spanIO)).toEqual({+ ...expected,+ span: spanIO,+ });+ });+});diff --git a/packages/shared/src/utils/normalized-io/conventions/providers/agno/fixtures.ts b/packages/shared/src/utils/normalized-io/conventions/providers/agno/fixtures.tsnew file mode 100644index 0000000000..754205b77f--- /dev/null+++ b/packages/shared/src/utils/normalized-io/conventions/providers/agno/fixtures.ts@@ -0,0 +1,21 @@+import type { NormalizedIOFixture } from "../fixture-types";++export const agnoPythonReprFixture = {+ name: "normalizes an Agno Python-repr message",+ spanIO: {+ input:+ "role='user' content='What is the weather?' name=None tool_call_id=None",+ output: undefined,+ metadata: undefined,+ },+ expected: {+ messages: [+ {+ role: "user",+ parts: [{ type: "text", text: "What is the weather?" }],+ source: "input",+ },+ ],+ toolDefinitions: [],+ },+} satisfies NormalizedIOFixture;diff --git a/packages/shared/src/utils/normalized-io/conventions/providers/agno/index.ts b/packages/shared/src/utils/normalized-io/conventions/providers/agno/index.tsnew file mode 100644index 0000000000..1e802d5bf9--- /dev/null+++ b/packages/shared/src/utils/normalized-io/conventions/providers/agno/index.ts@@ -0,0 +1,31 @@+import { claimed, unmatched } from "../..";+import type { ConventionResult, IOConvention } from "../../io-convention";++/**+ * Agno logs messages as Python reprs:+ * "role='user' content='...' name=None tool_call_id=None ...".+ * Bounded parse: role, then content up to the quote preceding the next+ * `field=`; anything the pattern doesn't match stays plain text.+ */+function parsePythonReprMessage(+ value: string,+): { role: string; content: string } | undefined {+ if (!value.startsWith("role='")) return undefined;++ const match =+ /^role='(\w+)' content=(?:'([\s\S]*?)'|"([\s\S]*?)")(?= \w+=)/.exec(value);+ if (!match) return undefined;++ const content = (match[2] ?? match[3] ?? "")+ .replace(/\\'/g, "'")+ .replace(/\\n/g, "\n");+ return { role: match[1], content };+}++export const agnoProvider = {+ name: "agno",+ tryPreprocessMessage: (value: string): ConventionResult<unknown> => {+ const message = parsePythonReprMessage(value);+ return message ? claimed(message) : unmatched;+ },+} satisfies IOConvention;diff --git a/packages/shared/src/utils/normalized-io/conventions/providers/ai-sdk/ai-sdk.test.ts b/packages/shared/src/utils/normalized-io/conventions/providers/ai-sdk/ai-sdk.test.tsnew file mode 100644index 0000000000..89f92d4ef7--- /dev/null+++ b/packages/shared/src/utils/normalized-io/conventions/providers/ai-sdk/ai-sdk.test.ts@@ -0,0 +1,19 @@+import { describe, expect, it } from "vitest";++import { normalizeSpanIO } from "../../../parser";+import {+ vercelAiSdkMixedToolMessagesFixture,+ vercelAiSdkOutputToolCallFixture,+} from "./fixtures";++describe("AI SDK normalized I/O", () => {+ it.each([+ vercelAiSdkMixedToolMessagesFixture,+ vercelAiSdkOutputToolCallFixture,+ ])("$name", ({ spanIO, expected }) => {+ expect(normalizeSpanIO(spanIO)).toEqual({+ ...expected,+ span: spanIO,+ });+ });+});diff --git a/packages/shared/src/utils/normalized-io/conventions/providers/ai-sdk/fixtures.ts b/packages/shared/src/utils/normalized-io/conventions/providers/ai-sdk/fixtures.tsnew file mode 100644index 0000000000..69961482e8--- /dev/null+++ b/packages/shared/src/utils/normalized-io/conventions/providers/ai-sdk/fixtures.ts@@ -0,0 +1,562 @@+import type { SpanIO, ToolDefinition } from "../../../types";++import type { NormalizedIOFixture } from "../fixture-types";++const inputToolCallId = "call_5iGKBMczvh1pevPChrZNGSFB";+const outputToolCallId = "toolu_01XXtujJ3DBaYEZGzn96xpGt";++const toolDefinitions: ToolDefinition[] = [+ {+ type: "function",+ name: "searchLangfuseDocs",+ description:+ "Semantic search (RAG) over the Langfuse documentation. Returns a concise answer synthesized from relevant docs.",+ inputSchema: {+ type: "object",+ properties: {+ query: {+ type: "string",+ description: "The user's question in natural language.",+ },+ },+ required: ["query"],+ additionalProperties: false,+ },+ },+ {+ type: "function",+ name: "getLangfuseDocsPage",+ description:+ "Fetch the raw Markdown for a single Langfuse docs page from a path or URL.",+ inputSchema: {+ type: "object",+ properties: {+ pathOrUrl: {+ type: "string",+ description: "A Langfuse docs path or full URL.",+ },+ },+ required: ["pathOrUrl"],+ additionalProperties: false,+ },+ },+ {+ type: "function",+ name: "getLangfuseOverview",+ description:+ "Get a high-level, machine-readable index of the Langfuse documentation.",+ inputSchema: {+ type: "object",+ properties: {},+ additionalProperties: false,+ },+ },+];++const inputMessages = [+ {+ role: "system",+ content:+ "You are a helpful Langfuse support agent. Give correct, actionable answers with links and minimal runnable examples.",+ },+ {+ role: "user",+ content: "What can I use Langfuse for?",+ },+ {+ role: "assistant",+ content: [+ {+ type: "reasoning",+ text: "I need to find out what Langfuse is used for and review the documentation overview first.",+ },+ {+ type: "tool-call",+ toolCallId: inputToolCallId,+ toolName: "getLangfuseOverview",+ input: {},+ },+ ],+ },+ {+ role: "tool",+ content: [+ {+ type: "tool-result",+ toolCallId: inputToolCallId,+ toolName: "getLangfuseOverview",+ output: {+ type: "content",+ value: [+ {+ type: "text",+ text: "[Langfuse documentation overview response]",+ },+ ],+ },+ },+ ],+ },+];++const outputMessage = {+ role: "user",+ content: [+ {+ type: "tool_use",+ id: outputToolCallId,+ name: "extract",+ input: {+ score: "0",+ reasoning:+ "The last user message is a how-to question asking for guidance on using Langfuse evaluation features. It does not express feedback about a feature.",+ },+ caller: { type: "direct" },+ },+ ],+};++const resourceAttributes = {+ "service.name": "unknown_service:node",+ "telemetry.sdk.language": "nodejs",+ "telemetry.sdk.name": "opentelemetry",+ "telemetry.sdk.version": "2.0.1",+};++const spanMetadata = {+ attributes: {+ "gen_ai.operation.name": "chat",+ "gen_ai.provider.name": "openai.responses",+ "gen_ai.request.model": "gpt-5-2025-08-07",+ "gen_ai.tool.definitions": JSON.stringify(toolDefinitions),+ },+ resourceAttributes,+ scope: { name: "ai" },+};++/**+ * Based on the ClickHouse row for an ai.streamText.doStream observation. The+ * fixture keeps the mixed message formats from the row while reducing the+ * large system prompt and documentation response to representative values.+ */+export const vercelAiSdkMixedToolMessagesFixture = {+ name: "normalizes mixed Vercel AI SDK tool messages",+ otel: {+ scopeSpan: {+ scope: { name: "ai" },+ spans: [+ {+ traceId: Buffer.from("a6babebc8be3faed5cbf76e3afb394f1", "hex"),+ spanId: Buffer.from("9c9f649e5c57e86c", "hex"),+ parentSpanId: Buffer.from("2cd1febe538594a6", "hex"),+ name: "ai.streamText.doStream",+ kind: 3,+ attributes: [+ {+ key: "gen_ai.operation.name",+ value: { stringValue: "chat" },+ },+ {+ key: "gen_ai.provider.name",+ value: { stringValue: "openai.responses" },+ },+ {+ key: "gen_ai.request.model",+ value: { stringValue: "gpt-5-2025-08-07" },+ },+ {+ key: "gen_ai.input.messages",+ value: { stringValue: JSON.stringify(inputMessages) },+ },+ {+ key: "gen_ai.output.messages",+ value: { stringValue: JSON.stringify(outputMessage) },+ },+ {+ key: "gen_ai.tool.definitions",+ value: { stringValue: JSON.stringify(toolDefinitions) },+ },+ ],+ events: [],+ status: { code: 0 },+ },+ ],+ },+ resourceAttributes,+ },+ spanIO: {+ input: JSON.stringify({ messages: inputMessages }),+ output: JSON.stringify(outputMessage),+ metadata: spanMetadata,+ } satisfies SpanIO,+ expected: {+ messages: [+ {+ role: "system",+ parts: [+ {+ type: "text",+ text: "You are a helpful Langfuse support agent. Give correct, actionable answers with links and minimal runnable examples.",+ },+ ],+ source: "input",+ },+ {+ role: "user",+ parts: [{ type: "text", text: "What can I use Langfuse for?" }],+ source: "input",+ },+ {+ role: "assistant",+ parts: [+ {+ type: "reasoning",+ content: {+ kind: "text",+ text: "I need to find out what Langfuse is used for and review the documentation overview first.",+ },+ },+ {+ type: "tool-call",+ toolCallId: inputToolCallId,+ toolName: "getLangfuseOverview",+ input: {},+ toolType: "tool-call",+ },+ ],+ source: "input",+ },+ {+ role: "tool",+ parts: [+ {+ type: "tool-result",+ toolCallId: inputToolCallId,+ toolName: "getLangfuseOverview",+ // AI SDK {type, value} output wrappers are unwrapped; the+ // structured value is the result.+ output: [+ {+ type: "text",+ text: "[Langfuse documentation overview response]",+ },+ ],+ },+ ],+ source: "input",+ },+ {+ role: "user",+ parts: [+ {+ type: "tool-call",+ toolCallId: outputToolCallId,+ toolName: "extract",+ input: {+ score: "0",+ reasoning:+ "The last user message is a how-to question asking for guidance on using Langfuse evaluation features. It does not express feedback about a feature.",+ },+ toolType: "tool_use",+ providerMetadata: { caller: { type: "direct" } },+ },+ ],+ source: "output",+ },+ ],+ toolDefinitions: toolDefinitions.map((definition) => ({+ name: definition.name,+ description: definition.description,+ inputSchema: definition.inputSchema,+ type: definition.type,+ })),+ },+} satisfies NormalizedIOFixture;++const previousToolCallId = "call_37WZP0DuTXwk6x43u5sz0WpD";+const outputToolCallIdFromRecovery = "call_CQBg5lwXHRCSONvKr6OT9znL";++const previousToolInput = {+ query:+ "RAG tracing Langfuse example retrieval generation evaluation datasets",+};++const outputToolInput = {+ query:+ "Langfuse Python minimal tracing example RAG retrieve span generation span OpenAI client langfuse.openai example run_experiment datasets quickstart",+};++const toolDefinition = {+ type: "function",+ name: "searchLangfuseDocs",+ inputSchema: {+ type: "object",+ properties: {+ query: {+ type: "string",+ description: "The user's question in natural language.",+ },+ },+ required: ["query"],+ additionalProperties: false,+ $schema: "http://json-schema.org/draft-07/schema#",+ },+ description: "Semantic search over the Langfuse documentation.",+};++const recoveryInputMessages = [+ {+ role: "user",+ parts: [{ type: "text", content: "Building rag" }],+ },+ {+ role: "assistant",+ parts: [+ {+ type: "text",+ content: "I'll search the Langfuse docs for a RAG example.",+ },+ {+ type: "tool_call",+ id: previousToolCallId,+ name: "searchLangfuseDocs",+ arguments: previousToolInput,+ },+ ],+ },+ {+ role: "tool",+ parts: [+ {+ type: "tool_call_response",+ id: previousToolCallId,+ response: {+ type: "content",+ value: [+ {+ type: "text",+ text: "[Reduced documentation search response]",+ },+ ],+ },+ },+ ],+ },+];++const outputMessages = [+ {+ role: "assistant",+ parts: [+ {+ type: "reasoning",+ content: "Looking into the RAG integration documentation.",+ },+ // Reasoning-generated file (AI SDK reasoning-file part, FilePart+ // nested under `file`).+ {+ type: "reasoning-file",+ file: {+ type: "file",+ data: "https://example.com/scratchpad.png",+ mediaType: "image/png",+ },+ },+ // Plain AI SDK file part (flat data | url shape).+ {+ type: "file",+ data: "UEsDBA==",+ mediaType: "application/zip",+ filename: "bundle.zip",+ },+ // Anchor-less document reference (AI SDK source part).+ {+ type: "source",+ sourceType: "url",+ id: "src_1",+ url: "https://example.com/rag-docs",+ title: "RAG integration docs",+ },+ {+ type: "tool_call",+ id: outputToolCallIdFromRecovery,+ name: "searchLangfuseDocs",+ arguments: outputToolInput,+ },+ ],+ finish_reason: "tool_call",+ },+];++const recoveryResourceAttributes = {+ "service.name": "unknown_service:node",+ "telemetry.sdk.language": "nodejs",+ "telemetry.sdk.name": "opentelemetry",+ "telemetry.sdk.version": "2.0.1",+};++const recoverySpanMetadata = {+ attributes: {+ "gen_ai.operation.name": "chat",+ "gen_ai.provider.name": "openai",+ "gen_ai.request.model": "gpt-5",+ "gen_ai.tool.definitions": JSON.stringify([toolDefinition]),+ },+ resourceAttributes: recoveryResourceAttributes,+ scope: { name: "gen_ai", attributes: {} },+};++/**+ * Captured from a production Vercel AI SDK trace:+ * trace 5f9b96b2d186c0cd172bc93272bf6f68,+ * span a661aa3c1be83d6a. Large text and tool-result payloads are reduced while+ * preserving the OTel message/tool structure that exposed the regression.+ */+export const vercelAiSdkOutputToolCallFixture = {+ name: "extracts an output tool call from GenAI message parts",+ otel: {+ scopeSpan: {+ scope: { name: "gen_ai" },+ spans: [+ {+ traceId: Buffer.from("5f9b96b2d186c0cd172bc93272bf6f68", "hex"),+ spanId: Buffer.from("a661aa3c1be83d6a", "hex"),+ parentSpanId: Buffer.from("e85de236034cf4a5", "hex"),+ name: "chat gpt-5",+ kind: 3,+ attributes: [+ {+ key: "gen_ai.operation.name",+ value: { stringValue: "chat" },+ },+ {+ key: "gen_ai.provider.name",+ value: { stringValue: "openai" },+ },+ {+ key: "gen_ai.request.model",+ value: { stringValue: "gpt-5" },+ },+ {+ key: "gen_ai.input.messages",+ value: { stringValue: JSON.stringify(recoveryInputMessages) },+ },+ {+ key: "gen_ai.tool.definitions",+ value: { stringValue: JSON.stringify([toolDefinition]) },+ },+ {+ key: "gen_ai.output.messages",+ value: { stringValue: JSON.stringify(outputMessages) },+ },+ ],+ events: [],+ status: { code: 0 },+ },+ ],+ },+ resourceAttributes: recoveryResourceAttributes,+ },+ spanIO: {+ input: JSON.stringify(recoveryInputMessages),+ output: JSON.stringify(outputMessages),+ metadata: recoverySpanMetadata,+ },+ expected: {+ messages: [+ {+ role: "user",+ parts: [{ type: "text", text: "Building rag" }],+ source: "input",+ },+ {+ role: "assistant",+ parts: [+ {+ type: "text",+ text: "I'll search the Langfuse docs for a RAG example.",+ },+ {+ type: "tool-call",+ toolCallId: previousToolCallId,+ toolName: "searchLangfuseDocs",+ input: previousToolInput,+ toolType: "tool_call",+ },+ ],+ source: "input",+ },+ {+ role: "tool",+ parts: [+ {+ type: "tool-result",+ toolCallId: previousToolCallId,+ output: [+ {+ type: "text",+ text: "[Reduced documentation search response]",+ },+ ],+ },+ ],+ source: "input",+ },+ {+ role: "assistant",+ parts: [+ {+ type: "reasoning",+ content: {+ kind: "text",+ text: "Looking into the RAG integration documentation.",+ },+ },+ {+ type: "file",+ mediaType: "image/png",+ content: { kind: "url", url: "https://example.com/scratchpad.png" },+ // Reasoning provenance is a typed field, not a part type.+ reasoning: true,+ },+ {+ type: "file",+ mediaType: "application/zip",+ filename: "bundle.zip",+ content: { kind: "base64", data: "UEsDBA==" },+ },+ {+ // Anchor-less citations stay stream-positioned parts; anchored+ // ones live on their text part's providerMetadata.citations.+ type: "custom",+ kind: "source",+ value: {+ type: "source",+ sourceType: "url",+ id: "src_1",+ url: "https://example.com/rag-docs",+ title: "RAG integration docs",+ },+ },+ {+ type: "tool-call",+ toolCallId: outputToolCallIdFromRecovery,+ toolName: "searchLangfuseDocs",+ input: outputToolInput,+ toolType: "tool_call",+ },+ ],+ finishReason: { type: "tool-calls", raw: "tool_call" },+ source: "output",+ },+ ],+ toolDefinitions: [+ {+ name: toolDefinition.name,+ description: toolDefinition.description,+ inputSchema: toolDefinition.inputSchema,+ type: toolDefinition.type,+ },+ ],+ },+} satisfies NormalizedIOFixture;diff --git a/packages/shared/src/utils/normalized-io/conventions/providers/ai-sdk/index.ts b/packages/shared/src/utils/normalized-io/conventions/providers/ai-sdk/index.tsnew file mode 100644index 0000000000..642b9d168d--- /dev/null+++ b/packages/shared/src/utils/normalized-io/conventions/providers/ai-sdk/index.ts@@ -0,0 +1,215 @@+import { claimed, unmatched } from "../..";+import {+ asRecord,+ compact,+ optionalString,+ toJsonValue,+} from "../../../core/utils/json";+import {+ filePartFromMediaReference,+ mediaTypeFromDataUri,+ parseMediaReference,+} from "../../../core/normalize/message-parts/media";+import { toolCallPart } from "../../../core/normalize/message-parts/tool-calls";+import { toolResultPart } from "../../../core/normalize/message-parts/tool-results";+import {+ toolDefinition,+ toolDefinitionProviderMetadata,+} from "../../../core/normalize/tool-definitions";+import type { FilePart, FinishReason } from "../../../types";+import type {+ IOConvention,+ PartHandler,+ ToolDefinitionCarrier,+ ToolDefinitionSource,+} from "../../io-convention";++/**+ * Vercel AI SDK convention: this module owns AI SDK's typed part vocabulary+ * and the `ai.prompt.tools` OTel attribute. `aiSdkFilePart` lives in+ * the canonical media builders because the `image`/`file` handlers use the+ * same media-reference mechanics.+ */++// The canonical FinishReason vocabulary is adopted from the AI SDK, so this+// map is the identity.+const AI_SDK_FINISH_REASON_TYPE_BY_RAW: Record<string, FinishReason["type"]> = {+ stop: "stop",+ length: "length",+ "tool-calls": "tool-calls",+ "content-filter": "content-filter",+ error: "error",+ other: "other",+ unknown: "unknown",+};++/**+ * AI SDK file payloads (`data`, legacy `image`, nested reasoning `file`):+ * raw base64 bytes, a URL string, or tagged {type: "data" | "url"} shapes.+ */+function aiSdkFilePart(+ payload: unknown,+ options: {+ mediaType?: string;+ filename?: string;+ fallbackMediaType?: string;+ } = {},+): FilePart | null {+ const tagged = asRecord(payload);+ const candidate = optionalString(tagged?.data ?? tagged?.url ?? payload);+ if (!candidate) return null;++ const reference = parseMediaReference(candidate);+ if (reference) {+ return compact({+ ...filePartFromMediaReference(reference),+ filename: options.filename,+ });+ }++ const isUrl =+ tagged?.type === "url" ||+ (tagged !== undefined && tagged.url !== undefined && !tagged.data) ||+ (!tagged && /^(https?:|data:)/.test(candidate));+ return compact<FilePart>({+ type: "file",+ mediaType:+ options.mediaType ??+ mediaTypeFromDataUri(candidate) ??+ options.fallbackMediaType,+ filename: options.filename,+ content: isUrl+ ? { kind: "url", url: candidate }+ : { kind: "base64", data: candidate },+ });+}++const normalizeAiSdkImage: PartHandler = (value) => {+ // Legacy AI SDK image parts carry the payload directly under `image`/+ // `data`; source-wrapped `image` blocks on the same type name belong to+ // the Anthropic dialect and fall through.+ if (asRecord(value.source)) return unmatched;+ const part = aiSdkFilePart(value.image ?? value.data, {+ mediaType: optionalString(value.mediaType),+ fallbackMediaType: "image/*",+ });+ return part ? claimed(part) : unmatched;+};++const normalizeAiSdkFile: PartHandler = (value) => {+ // AI SDK file parts carry a flat `data`/`url` payload; `file`-wrapped+ // blocks on the same type name belong to the OpenAI chat dialect and fall+ // through.+ if (asRecord(value.file)) return unmatched;+ const part = aiSdkFilePart(value.data ?? value.url, {+ mediaType: optionalString(value.mediaType),+ filename: optionalString(value.filename),+ });+ return part ? claimed(part) : unmatched;+};++const normalizeAiSdkToolError: PartHandler = (value) =>+ claimed({+ // AI SDK tool execution error: the error is the result.+ ...toolResultPart({+ toolCallId: value.toolCallId,+ toolName: value.toolName,+ output: value.error ?? null,+ }),+ isError: true,+ });++const normalizeAiSdkReasoningFile: PartHandler = (value) => {+ // AI SDK reasoning-generated file (a FilePart nested under `file`; older+ // emissions used a flat `data`): it is still a file to every consumer.+ const nested = asRecord(value.file);+ const part = aiSdkFilePart(nested?.data ?? nested?.url ?? value.data, {+ mediaType: optionalString(nested?.mediaType ?? value.mediaType),+ filename: optionalString(nested?.filename ?? value.filename),+ });+ return part ? claimed({ ...part, reasoning: true }) : unmatched;+};++const normalizeAiSdkSource: PartHandler = (value) =>+ claimed({+ // AI SDK source parts are stream-positioned references without a text+ // anchor, so they remain custom parts with their original payload.+ type: "custom",+ kind: "source",+ value: toJsonValue(value),+ });++// AI SDK tool-call parts: { toolCallId, toolName, input } (older emissions+// used `args`).+const normalizeAiSdkToolCall: PartHandler = (value) =>+ claimed(+ toolCallPart({+ toolCallId: value.toolCallId,+ toolName: value.toolName,+ input: value.input ?? value.args,+ toolType: optionalString(value.type),+ providerExecuted: value.providerExecuted,+ }),+ );+// AI SDK tool-result parts: { toolCallId, toolName, output } (v4 emissions+// used `result`).+const normalizeAiSdkToolResult: PartHandler = (value) =>+ claimed(+ toolResultPart({+ toolCallId: value.toolCallId,+ toolName: value.toolName,+ output: value.output ?? value.result,+ isError: typeof value.isError === "boolean" ? value.isError : undefined,+ }),+ );++const AI_SDK_PART_HANDLERS = {+ "tool-call": normalizeAiSdkToolCall,+ "tool-error": normalizeAiSdkToolError,+ "tool-result": normalizeAiSdkToolResult,+ "reasoning-file": normalizeAiSdkReasoningFile,+ source: normalizeAiSdkSource,+ image: normalizeAiSdkImage,+ file: normalizeAiSdkFile,+} satisfies Readonly<Record<string, PartHandler>>;++/**+ * The AI SDK's OTel instrumentation exports the request tools under the+ * `ai.prompt.tools` span attribute (provider built-ins may lack a name).+ */+function aiSdkToolDefinitionSources(+ carrier: ToolDefinitionCarrier,+): ToolDefinitionSource[] {+ const tools = carrier.metadataAttributes?.["ai.prompt.tools"];+ if (tools === undefined) return [];++ return [+ {+ sourceKey: "ai.prompt.tools",+ value: tools,+ options: { allowProviderToolWithoutName: true, allowToolMap: true },+ },+ ];+}++export const aiSdkProvider = {+ name: "ai-sdk",+ finishReasonTypeByRaw: AI_SDK_FINISH_REASON_TYPE_BY_RAW,+ // AI SDK / MCP tool declarations: { name?, description, inputSchema }.+ tryNormalizeToolDefinition: (value: Record<string, unknown>) => {+ if (value.inputSchema === undefined) return unmatched;+ const definition = toolDefinition({+ name: value.name,+ description: value.description,+ inputSchema: value.inputSchema,+ type: value.type,+ providerMetadata: toolDefinitionProviderMetadata(value, value),+ });+ return definition ? claimed(definition) : unmatched;+ },+ // UIMessages carry a `parts` array; camelCase `toolCalls` siblings appear+ // in AI-SDK-flavored (and koog) logging.+ messageLikeKeys: new Set(["parts", "toolCalls"]),+ typedParts: AI_SDK_PART_HANDLERS,+ collectToolDefinitionSources: aiSdkToolDefinitionSources,+} satisfies IOConvention;diff --git a/packages/shared/src/utils/normalized-io/conventions/providers/anthropic/anthropic.test.ts b/packages/shared/src/utils/normalized-io/conventions/providers/anthropic/anthropic.test.tsnew file mode 100644index 0000000000..c6c677a71f--- /dev/null+++ b/packages/shared/src/utils/normalized-io/conventions/providers/anthropic/anthropic.test.ts@@ -0,0 +1,19 @@+import { describe, expect, it } from "vitest";++import { normalizeSpanIO } from "../../../parser";+import {+ anthropicMessagesRawServerToolsAndMediaFixture,+ anthropicMessagesRichContentFixture,+} from "./fixtures";++describe("Anthropic normalized I/O", () => {+ it.each([+ anthropicMessagesRawServerToolsAndMediaFixture,+ anthropicMessagesRichContentFixture,+ ])("$name", ({ spanIO, expected }) => {+ expect(normalizeSpanIO(spanIO)).toEqual({+ ...expected,+ span: spanIO,+ });+ });+});diff --git a/packages/shared/src/utils/normalized-io/conventions/providers/anthropic/fixtures.ts b/packages/shared/src/utils/normalized-io/conventions/providers/anthropic/fixtures.tsnew file mode 100644index 0000000000..992a6a340f--- /dev/null+++ b/packages/shared/src/utils/normalized-io/conventions/providers/anthropic/fixtures.ts@@ -0,0 +1,589 @@+import type { NormalizedIOFixture } from "../fixture-types";++const searchSchema = {+ type: "object",+ properties: { query: { type: "string" } },+ required: ["query"],+};++const mediaReference =+ "@@@langfuseMedia:type=image/png|id=anthropic-image-1|source=base64@@@";++/** Raw Anthropic Messages API shape, including server tools and multimodal blocks. */+export const anthropicMessagesRawServerToolsAndMediaFixture = {+ name: "normalizes raw Anthropic system, server tools, documents, and citations",+ spanIO: {+ input: {+ system: [+ {+ type: "text",+ text: "You are a concise research assistant.",+ },+ ],+ messages: [+ {+ role: "user",+ content: [+ {+ type: "text",+ text: "Find the latest report.",+ citations: [+ { type: "char_location", cited_text: "latest report" },+ ],+ },+ {+ type: "image",+ source: {+ type: "base64",+ media_type: "image/png",+ data: "aGVsbG8=",+ },+ },+ { type: "image", source: { type: "url", url: mediaReference } },+ {+ type: "document",+ title: "Report",+ context: "Reference document",+ citations: [{ type: "page_location", page_number: 1 }],+ source: {+ type: "base64",+ media_type: "application/pdf",+ data: "JVBERi0=",+ },+ },+ {+ type: "document",+ source: {+ type: "text",+ media_type: "text/plain",+ data: "A plain text document.",+ },+ },+ { type: "container_upload", file_id: "file-container-1" },+ ],+ },+ {+ role: "assistant",+ content: [+ { type: "redacted_thinking", data: "opaque-thinking-data" },+ {+ type: "server_tool_use",+ id: "server-call-1",+ name: "web_search",+ input: { query: "latest report" },+ },+ {+ type: "mcp_tool_use",+ id: "mcp-call-1",+ name: "lookup_report",+ server_name: "reports",+ input: { id: "report-1" },+ },+ ],+ },+ {+ role: "user",+ content: [+ {+ type: "web_search_tool_result",+ tool_use_id: "server-call-1",+ content: [{ type: "text", text: "Search result" }],+ },+ {+ type: "tool_result",+ tool_use_id: "server-call-1",+ is_error: true,+ content: "The report service timed out.",+ },+ {+ type: "mcp_tool_result",+ tool_use_id: "mcp-call-1",+ is_error: false,+ content: "Report metadata",+ },+ ],+ },+ ],+ tools: [+ {+ name: "lookup_report",+ description: "Look up a report by ID.",+ input_schema: searchSchema,+ },+ {+ type: "web_search_20250305",+ name: "web_search",+ max_uses: 3,+ },+ ],+ },+ output: {+ role: "assistant",+ content: [+ {+ type: "text",+ text: "I found the report.",+ citations: [{ type: "page_location", page_number: 1 }],+ },+ {+ type: "tool_use",+ id: "output-call-1",+ name: "lookup_report",+ input: { id: "report-1" },+ },+ {+ type: "mcp_tool_use",+ id: "output-mcp-1",+ name: "save_report",+ server_name: "reports",+ input: { id: "report-1" },+ },+ ],+ stop_reason: "tool_use",+ },+ metadata: undefined,+ },+ expected: {+ messages: [+ {+ role: "system",+ parts: [+ { type: "text", text: "You are a concise research assistant." },+ ],+ source: "input",+ },+ {+ role: "user",+ parts: [+ {+ type: "text",+ text: "Find the latest report.",+ providerMetadata: {+ citations: [+ { type: "char_location", cited_text: "latest report" },+ ],+ },+ },+ {+ type: "file",+ mediaType: "image/png",+ content: { kind: "base64", data: "aGVsbG8=" },+ },+ {+ type: "file",+ mediaType: "image/png",+ content: { kind: "reference", id: "anthropic-image-1" },+ providerMetadata: { source: "base64" },+ },+ {+ type: "file",+ mediaType: "application/pdf",+ content: { kind: "base64", data: "JVBERi0=" },+ providerMetadata: {+ title: "Report",+ context: "Reference document",+ citations: [{ type: "page_location", page_number: 1 }],+ },+ },+ {+ type: "custom",+ kind: "document",+ value: {+ type: "document",+ source: {+ type: "text",+ media_type: "text/plain",+ data: "A plain text document.",+ },+ },+ },+ {+ type: "file",+ // Opaque reference β no media-type signal, so none is invented+ // (README assumption 11).+ content: { kind: "reference", id: "file-container-1" },+ },+ ],+ source: "input",+ },+ {+ role: "assistant",+ parts: [+ {+ type: "reasoning",+ content: { kind: "redacted", data: "opaque-thinking-data" },+ },+ {+ type: "tool-call",+ toolCallId: "server-call-1",+ toolName: "web_search",+ input: { query: "latest report" },+ toolType: "server_tool_use",+ providerExecuted: true,+ },+ {+ type: "tool-call",+ toolCallId: "mcp-call-1",+ toolName: "lookup_report",+ input: { id: "report-1" },+ toolType: "mcp_tool_use",+ providerExecuted: true,+ providerMetadata: { server_name: "reports" },+ },+ ],+ source: "input",+ },+ {+ role: "tool",+ parts: [+ {+ type: "tool-result",+ toolCallId: "server-call-1",+ output: [{ type: "text", text: "Search result" }],+ },+ {+ type: "tool-result",+ toolCallId: "server-call-1",+ output: "The report service timed out.",+ isError: true,+ },+ {+ type: "tool-result",+ toolCallId: "mcp-call-1",+ output: "Report metadata",+ isError: false,+ },+ ],+ source: "input",+ },+ {+ role: "assistant",+ parts: [+ {+ type: "text",+ text: "I found the report.",+ providerMetadata: {+ citations: [{ type: "page_location", page_number: 1 }],+ },+ },+ {+ type: "tool-call",+ toolCallId: "output-call-1",+ toolName: "lookup_report",+ input: { id: "report-1" },+ toolType: "tool_use",+ },+ {+ type: "tool-call",+ toolCallId: "output-mcp-1",+ toolName: "save_report",+ input: { id: "report-1" },+ toolType: "mcp_tool_use",+ providerExecuted: true,+ providerMetadata: { server_name: "reports" },+ },+ ],+ finishReason: { type: "tool-calls", raw: "tool_use" },+ source: "output",+ },+ ],+ toolDefinitions: [+ {+ name: "lookup_report",+ description: "Look up a report by ID.",+ inputSchema: searchSchema,+ },+ {+ name: "web_search",+ type: "web_search_20250305",+ providerMetadata: { max_uses: 3 },+ },+ ],+ },+} satisfies NormalizedIOFixture;++const inputToolCallId1 = "call_i1";+const inputToolCallId2 = "call_i2";+const outputToolCallId1 = "call_o1";+const outputToolCallId2 = "call_o2";++const searchDocsDefinition = {+ name: "search_docs",+ description: "Search internal documentation for a given query.",+ parameters: {+ type: "object",+ properties: {+ query: { type: "string", description: "The search query." },+ },+ required: ["query"],+ additionalProperties: false,+ },+};++const escalateDefinition = {+ name: "escalate_to_human",+ description: "Escalate the conversation to a human support agent.",+ parameters: {+ type: "object",+ properties: {+ reason: { type: "string", description: "Why escalation is needed." },+ },+ required: ["reason"],+ additionalProperties: false,+ },+};++const inputToolInput1 = { query: "current pricing tiers" };+const inputToolInput2 = { query: "recent pricing changes release notes" };+const outputToolInput1 = { query: "current pricing tiers documentation" };+const outputToolInput2 = { query: "Q3 release notes pricing changes" };++const inputMessages = [+ {+ role: "system",+ content:+ "You are a research assistant that answers by searching internal documentation before responding.",+ },+ {+ role: "user",+ content: "How does our pricing compare to the previous tier structure?",+ },+ {+ role: "assistant",+ content:+ "I can look into that β let me check the current and legacy pricing docs.",+ },+ {+ role: "user",+ content:+ "Please also check if there were any changes announced in the last release notes.",+ },+ {+ role: "assistant",+ content: [+ {+ index: 0,+ type: "thinking",+ thinking:+ "The user wants a comparison across two pricing versions plus recent release notes. I should search two sources in parallel: current pricing docs and release notes.",+ // Anthropic requires this signature to be replayed back unmodified on+ // the next turn for extended-thinking conversations. The parser has+ // nowhere to put it today (see the note below the fixture).+ signature: "sig_1a2b3c",+ },+ {+ index: 1,+ type: "tool_use",+ id: inputToolCallId1,+ name: "search_docs",+ input: JSON.stringify(inputToolInput1),+ caller: { type: "direct" },+ },+ {+ index: 2,+ type: "tool_use",+ id: inputToolCallId2,+ name: "search_docs",+ input: JSON.stringify(inputToolInput2),+ caller: { type: "direct" },+ },+ ],+ // Real captures carry both the native Anthropic content blocks and a+ // LangChain-style flattened tool_calls array referencing the same ids β+ // the parser must dedup these, not double-count.+ tool_calls: [+ {+ name: "search_docs",+ args: inputToolInput1,+ id: inputToolCallId1,+ type: "tool_call",+ },+ {+ name: "search_docs",+ args: inputToolInput2,+ id: inputToolCallId2,+ type: "tool_call",+ },+ ],+ },+];++const outputMessage = {+ role: "assistant",+ content: [+ {+ index: 0,+ type: "thinking",+ thinking:+ "Both searches are running in parallel to gather the current and legacy pricing details plus any recent release notes.",+ signature: "sig_4d5e6f",+ },+ {+ index: 1,+ type: "tool_use",+ id: outputToolCallId1,+ name: "search_docs",+ input: JSON.stringify(outputToolInput1),+ caller: { type: "direct" },+ },+ {+ index: 2,+ type: "tool_use",+ id: outputToolCallId2,+ name: "search_docs",+ input: JSON.stringify(outputToolInput2),+ caller: { type: "direct" },+ },+ ],+ tool_calls: [+ {+ id: outputToolCallId1,+ type: "tool_call",+ name: "search_docs",+ arguments: JSON.stringify(outputToolInput1),+ },+ {+ id: outputToolCallId2,+ type: "tool_call",+ name: "search_docs",+ arguments: JSON.stringify(outputToolInput2),+ },+ ],+};++/**+ * Anonymized shape from a real production trace (LangChain-wrapped Anthropic+ * extended-thinking call with parallel tool use), captured via+ * scripts/prepareOtelFixture.ts. Content is synthetic; the message/tool+ * structure β including the redundant content[]/tool_calls[] representation+ * that must dedup to one call per id β matches the original.+ */+export const anthropicMessagesRichContentFixture = {+ name: "normalizes Anthropic extended thinking with parallel tool use",+ spanIO: {+ input: JSON.stringify({+ messages: inputMessages,+ tools: [searchDocsDefinition, escalateDefinition],+ }),+ output: JSON.stringify(outputMessage),+ metadata: undefined,+ },+ expected: {+ messages: [+ {+ role: "system",+ parts: [+ {+ type: "text",+ text: "You are a research assistant that answers by searching internal documentation before responding.",+ },+ ],+ source: "input",+ },+ {+ role: "user",+ parts: [+ {+ type: "text",+ text: "How does our pricing compare to the previous tier structure?",+ },+ ],+ source: "input",+ },+ {+ role: "assistant",+ parts: [+ {+ type: "text",+ text: "I can look into that β let me check the current and legacy pricing docs.",+ },+ ],+ source: "input",+ },+ {+ role: "user",+ parts: [+ {+ type: "text",+ text: "Please also check if there were any changes announced in the last release notes.",+ },+ ],+ source: "input",+ },+ {+ role: "assistant",+ parts: [+ {+ type: "reasoning",+ content: {+ kind: "text",+ text: "The user wants a comparison across two pricing versions plus recent release notes. I should search two sources in parallel: current pricing docs and release notes.",+ signature: "sig_1a2b3c",+ },+ providerMetadata: { index: 0 },+ },+ {+ type: "tool-call",+ toolCallId: inputToolCallId1,+ toolName: "search_docs",+ input: inputToolInput1,+ toolType: "tool_use",+ providerMetadata: { index: 1, caller: { type: "direct" } },+ },+ {+ type: "tool-call",+ toolCallId: inputToolCallId2,+ toolName: "search_docs",+ input: inputToolInput2,+ toolType: "tool_use",+ providerMetadata: { index: 2, caller: { type: "direct" } },+ },+ ],+ source: "input",+ },+ {+ role: "assistant",+ parts: [+ {+ type: "reasoning",+ content: {+ kind: "text",+ text: "Both searches are running in parallel to gather the current and legacy pricing details plus any recent release notes.",+ signature: "sig_4d5e6f",+ },+ providerMetadata: { index: 0 },+ },+ {+ type: "tool-call",+ toolCallId: outputToolCallId1,+ toolName: "search_docs",+ input: outputToolInput1,+ toolType: "tool_use",+ providerMetadata: { index: 1, caller: { type: "direct" } },+ },+ {+ type: "tool-call",+ toolCallId: outputToolCallId2,+ toolName: "search_docs",+ input: outputToolInput2,+ toolType: "tool_use",+ providerMetadata: { index: 2, caller: { type: "direct" } },+ },+ ],+ source: "output",+ },+ ],+ toolDefinitions: [+ {+ name: "search_docs",+ description: "Search internal documentation for a given query.",+ inputSchema: searchDocsDefinition.parameters,+ },+ {+ name: "escalate_to_human",+ description: "Escalate the conversation to a human support agent.",+ inputSchema: escalateDefinition.parameters,+ },+ ],+ },+} satisfies NormalizedIOFixture;diff --git a/packages/shared/src/utils/normalized-io/conventions/providers/anthropic/index.ts b/packages/shared/src/utils/normalized-io/conventions/providers/anthropic/index.tsnew file mode 100644index 0000000000..722ab984da--- /dev/null+++ b/packages/shared/src/utils/normalized-io/conventions/providers/anthropic/index.ts@@ -0,0 +1,249 @@+import { claimed, unmatched } from "../..";+import {+ asRecord,+ compact,+ optionalString,+ toJsonValue,+ toProviderMetadata,+} from "../../../core/utils/json";+import {+ filePartFromUrl,+ type UrlFilePartOptions,+} from "../../../core/normalize/message-parts/media";+import { reasoningPart } from "../../../core/normalize/message-parts/reasoning";+import {+ providerExecutedToolCall,+ toolCallPart,+} from "../../../core/normalize/message-parts/tool-calls";+import { toolResultPart } from "../../../core/normalize/message-parts/tool-results";+import {+ toolDefinition,+ toolDefinitionProviderMetadata,+} from "../../../core/normalize/tool-definitions";+import type { FilePart, FinishReason } from "../../../types";+import type {+ IOConvention,+ PartHandler,+ MessageSource,+} from "../../io-convention";++/**+ * Anthropic Messages API convention: this module owns Anthropic's typed+ * block vocabulary, its `system` request field, and the `thinking` sibling+ * array. One message can still contain parts from several dialects β the+ * core folds every provider cumulatively.+ */++// Anthropic `stop_reason` vocabulary -> the canonical FinishReason set.+const ANTHROPIC_FINISH_REASON_TYPE_BY_RAW: Record<+ string,+ FinishReason["type"]+> = {+ end_turn: "stop",+ stop_sequence: "stop",+ max_tokens: "length",+ tool_use: "tool-calls",+ refusal: "content-filter",+ pause_turn: "other",+};++const ANTHROPIC_TOOL_RESULT_PART_TYPES = [+ "tool_result",+ "web_search_tool_result",+ "code_execution_tool_result",+ "bash_code_execution_result",+ "text_editor_code_execution_tool_result",+ "text_editor_code_execution_view_result",+ "mcp_tool_result",+];++/** Anthropic `source` objects: `{type: "base64" | "url" | "file", ...}`. */+function filePartFromAnthropicSource(+ source: Record<string, unknown>,+ options: Omit<UrlFilePartOptions, "mediaType"> & {+ extras?: Record<string, unknown>;+ } = {},+): FilePart | null {+ const mediaType = optionalString(source.media_type);++ const url = optionalString(source.url);+ if (source.type === "url" && url) {+ return filePartFromUrl(url, { ...options, mediaType });+ }++ const data = optionalString(source.data);+ const fileId = optionalString(source.file_id);+ const content: FilePart["content"] | undefined =+ source.type === "base64" && data+ ? { kind: "base64", data }+ : source.type === "file" && fileId+ ? { kind: "reference", id: fileId }+ : undefined;+ if (!content) return null;++ return compact<FilePart>({+ type: "file",+ mediaType: mediaType ?? options.fallbackMediaType,+ content,+ providerMetadata: options.extras+ ? toProviderMetadata(options.extras)+ : undefined,+ });+}++const normalizeAnthropicImage: PartHandler = (value) => {+ // Anthropic image blocks carry a `source`; source-less `image` parts on+ // the same type name belong to the AI SDK dialect and fall through.+ const source = asRecord(value.source);+ if (!source) return unmatched;+ const part = filePartFromAnthropicSource(source, {+ fallbackMediaType: "image/*",+ });+ return part ? claimed(part) : unmatched;+};++// Anthropic call blocks: { type, id, name, input }. Streamed emissions+// also carry the content-block `index` β it stays in providerMetadata.+const anthropicToolCall = (value: Record<string, unknown>, toolType?: string) =>+ toolCallPart({+ toolCallId: value.id,+ toolName: value.name,+ input: value.input,+ toolType,+ });++const normalizeAnthropicServerToolCall: PartHandler = (value) =>+ claimed(+ providerExecutedToolCall(anthropicToolCall(value, "server_tool_use")),+ );++const normalizeAnthropicMcpToolCall: PartHandler = (value) =>+ claimed(+ providerExecutedToolCall(+ anthropicToolCall(value, "mcp_tool_use"),+ compact({ server_name: optionalString(value.server_name) }),+ ),+ );++const normalizeAnthropicThinking: PartHandler = (value) =>+ claimed(reasoningPart(value.thinking, optionalString(value.signature)));++const normalizeAnthropicRedactedThinking: PartHandler = (value) => {+ const data = optionalString(value.data);+ return data+ ? claimed({ type: "reasoning", content: { kind: "redacted", data } })+ : claimed(reasoningPart(value.data ?? null));+};++const normalizeAnthropicDocument: PartHandler = (value) => {+ const source = asRecord(value.source);+ const part = source+ ? filePartFromAnthropicSource(source, {+ extras: compact({+ title: optionalString(value.title),+ context: optionalString(value.context),+ citations: Array.isArray(value.citations)+ ? value.citations+ : undefined,+ }),+ })+ : null;+ if (part) return claimed(part);++ // Text and structured-content documents are semantic content, but do not+ // have a file reference that a renderer can resolve.+ return claimed({+ type: "custom",+ kind: "document",+ value: toJsonValue(value),+ });+};++const normalizeAnthropicContainerUpload: PartHandler = (value) => {+ const fileId = optionalString(value.file_id);+ if (!fileId) return unmatched;+ // Opaque reference: no media-type signal (README assumption 11).+ return claimed({ type: "file", content: { kind: "reference", id: fileId } });+};++// Anthropic result blocks: { type: "tool_result" | "*_tool_result",+// tool_use_id, content, is_error? }.+const normalizeAnthropicToolResult: PartHandler = (value) =>+ claimed(+ toolResultPart({+ toolCallId: value.tool_use_id ?? value.id,+ output: value.content,+ isError: typeof value.is_error === "boolean" ? value.is_error : undefined,+ }),+ );++const ANTHROPIC_PART_HANDLERS = {+ tool_use: (value) => claimed(anthropicToolCall(value, "tool_use")),+ image: normalizeAnthropicImage,+ server_tool_use: normalizeAnthropicServerToolCall,+ mcp_tool_use: normalizeAnthropicMcpToolCall,+ thinking: normalizeAnthropicThinking,+ redacted_thinking: normalizeAnthropicRedactedThinking,+ document: normalizeAnthropicDocument,+ container_upload: normalizeAnthropicContainerUpload,+ ...Object.fromEntries(+ ANTHROPIC_TOOL_RESULT_PART_TYPES.map((type) => [+ type,+ normalizeAnthropicToolResult,+ ]),+ ),+} satisfies Readonly<Record<string, PartHandler>>;++/**+ * Anthropic's `system` request field: a string or content-block array,+ * carried beside `content` rather than in the message stream. Wrapped as+ * message content and forced to the system role.+ */+function anthropicSystemMessage(+ root: Record<string, unknown>,+ kind: "input" | "output",+): MessageSource | undefined {+ if (kind !== "input" || !("system" in root)) return undefined;+ return {+ kind: "single",+ value: { content: root.system },+ fallbackRole: "user",+ roleOverride: "system",+ };+}++// Anthropic carries citations under `citations` on text parts and document+// sources.+const ANTHROPIC_CITATION_KEYS = new Set(["citations"]);++export const anthropicProvider = {+ name: "anthropic",+ finishReasonTypeByRaw: ANTHROPIC_FINISH_REASON_TYPE_BY_RAW,+ typedParts: ANTHROPIC_PART_HANDLERS,+ citationKeys: ANTHROPIC_CITATION_KEYS,+ // Anthropic tool declarations: { name, description, input_schema }.+ tryNormalizeToolDefinition: (value: Record<string, unknown>) => {+ if (value.input_schema === undefined) return unmatched;+ const definition = toolDefinition({+ name: value.name,+ description: value.description,+ inputSchema: value.input_schema,+ type: value.type,+ providerMetadata: toolDefinitionProviderMetadata(value, value),+ });+ return definition ? claimed(definition) : unmatched;+ },+ collectSiblingParts: (+ value: Record<string, unknown>,+ _baseParts,+ context,+ ) => {+ const parts = context.normalizePartList(+ Array.isArray(value.thinking) ? value.thinking : [],+ );+ return parts.length > 0+ ? [{ sourceKey: "thinking", slot: "after-content", parts }]+ : [];+ },+ getSystemMessage: anthropicSystemMessage,+} satisfies IOConvention;diff --git a/packages/shared/src/utils/normalized-io/conventions/providers/fixture-types.ts b/packages/shared/src/utils/normalized-io/conventions/providers/fixture-types.tsnew file mode 100644index 0000000000..a16ccc90c3--- /dev/null+++ b/packages/shared/src/utils/normalized-io/conventions/providers/fixture-types.ts@@ -0,0 +1,14 @@+import type { ResourceSpan } from "../../../../server/otel/OtelIngestionProcessor";+import type { NormalizedIO, SpanIO } from "../../types";++type OtelScopeSpan = NonNullable<ResourceSpan["scopeSpans"]>[number];++export type NormalizedIOFixture = {+ name: string;+ otel?: {+ scopeSpan: OtelScopeSpan;+ resourceAttributes: Record<string, unknown>;+ };+ spanIO: SpanIO;+ expected: Omit<NormalizedIO, "span">;+};diff --git a/packages/shared/src/utils/normalized-io/conventions/providers/gemini/fixtures.ts b/packages/shared/src/utils/normalized-io/conventions/providers/gemini/fixtures.tsnew file mode 100644index 0000000000..ce17129051--- /dev/null+++ b/packages/shared/src/utils/normalized-io/conventions/providers/gemini/fixtures.ts@@ -0,0 +1,202 @@+import type { NormalizedIOFixture } from "../fixture-types";++/** Synthetic Gemini case adapted from the playground suite. */+export const geminiEmbeddedToolDefinitionFixture = {+ name: "extracts Gemini tool-definition messages",+ spanIO: {+ input: [+ { role: "system", content: "Use tools when they are helpful." },+ {+ role: "model",+ content: [{ type: "text", text: "How can I help?" }],+ },+ {+ role: "tool",+ content: {+ type: "function",+ function: {+ name: "get_weather",+ description: "Get the current weather in a city",+ parameters: {+ type: "object",+ properties: { city: { type: "string" } },+ required: ["city"],+ },+ },+ },+ },+ { role: "user", content: "What can you do?" },+ ],+ output: undefined,+ metadata: { provider: "google_vertexai" },+ },+ expected: {+ messages: [+ {+ role: "system",+ parts: [{ type: "text", text: "Use tools when they are helpful." }],+ source: "input",+ },+ {+ role: "assistant",+ parts: [{ type: "text", text: "How can I help?" }],+ source: "input",+ },+ {+ role: "user",+ parts: [{ type: "text", text: "What can you do?" }],+ source: "input",+ },+ ],+ toolDefinitions: [+ {+ name: "get_weather",+ description: "Get the current weather in a city",+ inputSchema: {+ type: "object",+ properties: { city: { type: "string" } },+ required: ["city"],+ },+ type: "function",+ },+ ],+ },+} satisfies NormalizedIOFixture;++/** Gemini system instructions can sit beside a generic message carrier. */+export const geminiSystemInstructionWithGenericMessagesFixture = {+ name: "keeps a Gemini system instruction beside generic messages",+ spanIO: {+ input: {+ messages: [{ role: "user", content: "Help me plan a trip." }],+ config: {+ system_instruction: {+ parts: [{ text: "Keep the answer concise." }],+ },+ },+ },+ output: undefined,+ metadata: undefined,+ },+ expected: {+ messages: [+ {+ role: "system",+ parts: [{ type: "text", text: "Keep the answer concise." }],+ source: "input",+ },+ {+ role: "user",+ parts: [{ type: "text", text: "Help me plan a trip." }],+ source: "input",+ },+ ],+ toolDefinitions: [],+ },+} satisfies NormalizedIOFixture;++/**+ * Raw Gemini/Vertex wire shapes: keyed parts without a `type` discriminator+ * (bare text, inline_data, file_data), thought parts with thoughtSignature,+ * provider-executed code execution (executable_code / code_execution_result),+ * and the candidate-level finishReason.+ */+export const geminiMediaAndCodeExecutionFixture = {+ name: "normalizes Gemini media parts and code execution",+ spanIO: {+ input: {+ contents: [+ {+ role: "user",+ parts: [+ { text: "Analyze this chart and compute the sum." },+ { inline_data: { mime_type: "image/png", data: "aVZCT1J3" } },+ {+ file_data: {+ mime_type: "application/pdf",+ file_uri: "gs://bucket/report.pdf",+ },+ },+ ],+ },+ ],+ },+ output: {+ candidates: [+ {+ content: {+ role: "model",+ parts: [+ {+ text: "Let me run the numbers.",+ thought: true,+ thoughtSignature: "sig_gemini_1",+ },+ {+ executable_code: { language: "PYTHON", code: "print(1 + 2)" },+ },+ {+ code_execution_result: { outcome: "OUTCOME_OK", output: "3" },+ },+ { text: "The sum is 3." },+ ],+ },+ finishReason: "STOP",+ },+ ],+ },+ metadata: undefined,+ },+ expected: {+ messages: [+ {+ role: "user",+ parts: [+ { type: "text", text: "Analyze this chart and compute the sum." },+ {+ type: "file",+ mediaType: "image/png",+ content: { kind: "base64", data: "aVZCT1J3" },+ },+ {+ type: "file",+ mediaType: "application/pdf",+ content: { kind: "url", url: "gs://bucket/report.pdf" },+ },+ ],+ source: "input",+ },+ {+ role: "assistant",+ parts: [+ {+ type: "reasoning",+ content: {+ kind: "text",+ text: "Let me run the numbers.",+ signature: "sig_gemini_1",+ },+ },+ {+ type: "tool-call",+ toolCallId: null,+ toolName: "code_execution",+ input: { language: "PYTHON", code: "print(1 + 2)" },+ toolType: "executable_code",+ providerExecuted: true,+ },+ {+ type: "tool-result",+ toolCallId: null,+ toolName: "code_execution",+ output: { outcome: "OUTCOME_OK", output: "3" },+ },+ { type: "text", text: "The sum is 3." },+ ],+ finishReason: { type: "stop", raw: "STOP" },+ source: "output",+ },+ ],+ toolDefinitions: [],+ },+} satisfies NormalizedIOFixture;diff --git a/packages/shared/src/utils/normalized-io/conventions/providers/gemini/gemini.test.ts b/packages/shared/src/utils/normalized-io/conventions/providers/gemini/gemini.test.tsnew file mode 100644index 0000000000..cbe0019a35--- /dev/null+++ b/packages/shared/src/utils/normalized-io/conventions/providers/gemini/gemini.test.ts@@ -0,0 +1,21 @@+import { describe, expect, it } from "vitest";++import { normalizeSpanIO } from "../../../parser";+import {+ geminiEmbeddedToolDefinitionFixture,+ geminiMediaAndCodeExecutionFixture,+ geminiSystemInstructionWithGenericMessagesFixture,+} from "./fixtures";++describe("Gemini normalized I/O", () => {+ it.each([+ geminiEmbeddedToolDefinitionFixture,+ geminiSystemInstructionWithGenericMessagesFixture,+ geminiMediaAndCodeExecutionFixture,+ ])("$name", ({ spanIO, expected }) => {+ expect(normalizeSpanIO(spanIO)).toEqual({+ ...expected,+ span: spanIO,+ });+ });+});diff --git a/packages/shared/src/utils/normalized-io/conventions/providers/gemini/index.ts b/packages/shared/src/utils/normalized-io/conventions/providers/gemini/index.tsnew file mode 100644index 0000000000..e7f3ca21ce--- /dev/null+++ b/packages/shared/src/utils/normalized-io/conventions/providers/gemini/index.ts@@ -0,0 +1,283 @@+import { claimed, unmatched } from "../..";+import {+ asRecord,+ compact,+ optionalString,+ parseArray,+ toJsonValue,+} from "../../../core/utils/json";+import {+ filePartFromMediaReference,+ filePartFromUrl,+ parseMediaReference,+} from "../../../core/normalize/message-parts/media";+import { reasoningPart } from "../../../core/normalize/message-parts/reasoning";+import { toolCallPart } from "../../../core/normalize/message-parts/tool-calls";+import { toolResultPart } from "../../../core/normalize/message-parts/tool-results";+import type {+ FilePart,+ FinishReason,+ NormalizedMessagePart,+ ToolResultPart,+} from "../../../types";+import type {+ ConventionResult,+ IOConvention,+ PartHandlerContext,+ MessageSource,+ ToolDefinitionCarrier,+ ToolDefinitionSource,+} from "../../io-convention";++/**+ * Gemini / Vertex convention. Gemini parts are keyed unions without a `type`+ * discriminator (functionCall, inlineData, fileData, executableCode,+ * codeExecutionResult, bare thought text) β recognized only when `type` is+ * absent. This module also owns the `contents`/`system_instruction`/+ * `candidates` container discovery and `config.tools` definitions.+ */++// Gemini `finishReason` vocabulary (reported uppercase; lookups are+// lowercased) -> the canonical FinishReason set.+const GEMINI_FINISH_REASON_TYPE_BY_RAW: Record<string, FinishReason["type"]> = {+ stop: "stop",+ max_tokens: "length",+ safety: "content-filter",+ recitation: "content-filter",+ blocklist: "content-filter",+ prohibited_content: "content-filter",+ spii: "content-filter",+ malformed_function_call: "error",+ other: "other",+};++/**+ * Keyed-union parts without a `type` discriminator: Gemini's keyed parts in+ * both SDK casings, plus the legacy function_call/function_response wrappers.+ */+function normalizeGeminiPart(+ value: Record<string, unknown>,+ _context: PartHandlerContext,+): ConventionResult<NormalizedMessagePart> {+ if (typeof value.type === "string") return unmatched;++ const functionCall =+ asRecord(value.function_call) ?? asRecord(value.functionCall);+ if (functionCall) {+ // Gemini functionCall: { name, args } (id sometimes on the wrapper).+ const part = toolCallPart({+ toolCallId: functionCall.id ?? value.id,+ toolName: functionCall.name,+ input: functionCall.args ?? functionCall.arguments,+ toolType: "functionCall",+ });+ return part ? claimed(part) : unmatched;+ }++ const functionResponse =+ asRecord(value.function_response) ?? asRecord(value.functionResponse);+ if (functionResponse) {+ // Gemini functionResponse: { name, response } β the name doubles as the+ // call id fallback, not the tool name (parity with the original join).+ return claimed(+ toolResultPart({+ toolCallId: functionResponse.id ?? functionResponse.name ?? value.id,+ output: functionResponse.response,+ }),+ );+ }++ const inlineData = asRecord(value.inline_data) ?? asRecord(value.inlineData);+ if (inlineData) {+ const data = optionalString(inlineData.data);+ if (data) {+ const reference = parseMediaReference(data);+ if (reference) return claimed(filePartFromMediaReference(reference));+ return claimed(+ compact<FilePart>({+ type: "file",+ mediaType: optionalString(+ inlineData.mime_type ?? inlineData.mimeType,+ ),+ content: { kind: "base64", data },+ }),+ );+ }+ }++ // Record shape only: OpenAI's `file_data` is a base64 string and belongs+ // to the typed file/input_file cases.+ const geminiFileData = asRecord(value.file_data) ?? asRecord(value.fileData);+ if (geminiFileData) {+ const fileUri = optionalString(+ geminiFileData.file_uri ?? geminiFileData.fileUri,+ );+ if (fileUri) {+ return claimed(+ filePartFromUrl(fileUri, {+ mediaType: optionalString(+ geminiFileData.mime_type ?? geminiFileData.mimeType,+ ),+ }),+ );+ }+ }++ const executableCode =+ asRecord(value.executable_code) ?? asRecord(value.executableCode);+ if (executableCode) {+ return claimed({+ type: "tool-call",+ toolCallId: null,+ toolName: "code_execution",+ input: toJsonValue(executableCode),+ toolType: "executable_code",+ providerExecuted: true,+ });+ }++ const codeExecutionResult =+ asRecord(value.code_execution_result) ??+ asRecord(value.codeExecutionResult);+ if (codeExecutionResult) {+ const outcome = optionalString(codeExecutionResult.outcome);+ return claimed(+ compact<ToolResultPart>({+ type: "tool-result",+ toolCallId: null,+ toolName: "code_execution",+ output: toJsonValue(codeExecutionResult),+ isError: outcome && outcome !== "OUTCOME_OK" ? true : undefined,+ }),+ );+ }++ // Gemini text/thought parts: a bare `text` field, optionally flagged as+ // thought with a signature sibling.+ if (typeof value.text === "string") {+ const signature = optionalString(+ value.thoughtSignature ?? value.thought_signature,+ );+ return claimed(+ value.thought === true || signature+ ? reasoningPart(value.text, signature)+ : { type: "text", text: value.text },+ );+ }++ return unmatched;+}++/**+ * Gemini requests declare tools under `config.tools`: named functions in+ * `function_declarations` groups (either casing), everything else being+ * provider built-ins (googleSearch, codeExecution, ...) that declare no+ * name. Discovery only β the core normalizes and merges the sources.+ */+function geminiToolDefinitionSources(+ carrier: ToolDefinitionCarrier,+): ToolDefinitionSource[] {+ const tools = parseArray(asRecord(carrier.root?.config)?.tools);+ if (!tools) return [];++ return tools.map((tool, index) => {+ const toolGroup = asRecord(tool);+ const declarations =+ parseArray(toolGroup?.function_declarations) ??+ parseArray(toolGroup?.functionDeclarations);+ if (declarations) {+ return {+ sourceKey: `config.tools[${index}].function_declarations`,+ value: declarations,+ options: { allowToolMap: true },+ };+ }+ return {+ sourceKey: `config.tools[${index}]`,+ value: tool,+ options: { allowProviderToolWithoutName: true, allowToolMap: true },+ };+ });+}++/**+ * Gemini's request container: `contents` (+ `config.system_instruction`)+ * on input, `candidates` (each with its own finish reason) on output.+ */+function geminiSystemMessage(+ root: Record<string, unknown>,+ kind: "input" | "output",+): MessageSource | undefined {+ if (kind !== "input") return undefined;++ const config = asRecord(root.config);+ const systemInstruction =+ config?.system_instruction ?? config?.systemInstruction;+ if (!systemInstruction) return undefined;++ return {+ kind: "single",+ value: systemInstruction,+ fallbackRole: "user",+ roleOverride: "system",+ };+}++function geminiMessages(+ root: Record<string, unknown>,+ kind: "input" | "output",+): MessageSource[] {+ const sources: MessageSource[] = [];++ if (kind === "input") {+ if ("new_message" in root) {+ sources.push({+ kind: "single",+ value: root.new_message,+ fallbackRole: "user",+ });+ return sources;+ }++ if ("contents" in root) {+ const contents = Array.isArray(root.contents)+ ? root.contents+ : [root.contents];+ sources.push({+ kind: "sequence",+ values: contents,+ fallbackRole: "user",+ });+ }+ }++ if (kind === "output") {+ const candidates = parseArray(root.candidates);+ if (candidates) {+ for (const candidate of candidates) {+ const candidateRecord = asRecord(candidate);+ sources.push({+ kind: "single",+ value: candidateRecord?.content,+ fallbackRole: "assistant",+ // Gemini reports the finish reason on the candidate, not the content.+ finishReasonCarrier: candidateRecord,+ });+ }+ }+ }++ return sources;+}++export const geminiProvider = {+ name: "gemini",+ finishReasonTypeByRaw: GEMINI_FINISH_REASON_TYPE_BY_RAW,+ roleByRawRole: { model: "assistant" },+ // Gemini contents carry their blocks in a `parts` array.+ messageLikeKeys: new Set(["parts"]),+ tryNormalizeUntypedPart: normalizeGeminiPart,+ claimMessages: geminiMessages,+ getSystemMessage: geminiSystemMessage,+ collectToolDefinitionSources: geminiToolDefinitionSources,+} satisfies IOConvention;diff --git a/packages/shared/src/utils/normalized-io/conventions/providers/langchain/fixtures.ts b/packages/shared/src/utils/normalized-io/conventions/providers/langchain/fixtures.tsnew file mode 100644index 0000000000..f491844cc5--- /dev/null+++ b/packages/shared/src/utils/normalized-io/conventions/providers/langchain/fixtures.ts@@ -0,0 +1,538 @@+import type { NormalizedIOFixture } from "../fixture-types";++/**+ * LangChain serialization envelope: instrumentation that dumps LangChain+ * message objects (dumpd) wraps each message in constructor kwargs with the+ * class path in `id`. Covers role derivation from the class name, tool calls+ * inside kwargs, invalid_tool_calls (kept as flagged tool calls, excluded+ * from tool columns), and finish_reason nested under response_metadata.+ * tool_call_chunks are deliberately ignored (streaming deltas, redundant+ * with tool_calls β see README).+ */+export const langchainSerializedEnvelopeFixture = {+ name: "normalizes LangChain serialized message envelopes",+ spanIO: {+ input: JSON.stringify([+ {+ lc: 1,+ type: "constructor",+ id: ["langchain_core", "messages", "SystemMessage"],+ kwargs: { content: "You are a weather assistant." },+ },+ {+ lc: 1,+ type: "constructor",+ id: ["langchain_core", "messages", "HumanMessage"],+ kwargs: { content: "What is the weather in Zurich?" },+ },+ ]),+ output: JSON.stringify([+ {+ lc: 1,+ type: "constructor",+ id: ["langchain_core", "messages", "AIMessage"],+ kwargs: {+ content: "",+ tool_calls: [+ {+ id: "call_lc_1",+ name: "get_weather",+ args: { city: "Zurich" },+ type: "tool_call",+ },+ ],+ invalid_tool_calls: [+ {+ id: "call_lc_2",+ name: "get_weather",+ args: '{"city":',+ error: "Malformed args.",+ type: "invalid_tool_call",+ },+ ],+ response_metadata: { finish_reason: "tool_calls" },+ },+ },+ {+ lc: 1,+ type: "constructor",+ id: ["langchain_core", "messages", "ToolMessage"],+ kwargs: {+ content: "Weather service unavailable.",+ tool_call_id: "call_lc_1",+ status: "error",+ artifact: { attempts: 2 },+ },+ },+ ]),+ metadata: undefined,+ },+ expected: {+ messages: [+ {+ role: "system",+ parts: [{ type: "text", text: "You are a weather assistant." }],+ source: "input",+ },+ {+ role: "user",+ parts: [{ type: "text", text: "What is the weather in Zurich?" }],+ source: "input",+ },+ {+ role: "assistant",+ parts: [+ {+ type: "tool-call",+ toolCallId: "call_lc_1",+ toolName: "get_weather",+ input: { city: "Zurich" },+ toolType: "tool_call",+ },+ {+ type: "tool-call",+ toolCallId: "call_lc_2",+ toolName: "get_weather",+ // Arguments stay the raw unparsable string.+ input: '{"city":',+ invalid: true,+ providerMetadata: { error: "Malformed args." },+ },+ ],+ finishReason: { type: "tool-calls", raw: "tool_calls" },+ source: "output",+ },+ {+ role: "tool",+ parts: [+ {+ type: "tool-result",+ toolCallId: "call_lc_1",+ output: "Weather service unavailable.",+ isError: true,+ // Side-band artifact data, preserved without becoming output.+ providerMetadata: { artifact: { attempts: 2 } },+ },+ ],+ source: "output",+ },+ ],+ toolDefinitions: [],+ },+} satisfies NormalizedIOFixture;++/**+ * LangChain dict serialization (`.dict()` / LangSmith-style): messages carry+ * `type: "tool"` instead of a `role` key. A ToolMessage dict with a+ * tool_call_id must become a tool-result part β not a text part titled by+ * `name` β and `name` is the tool's name, never a participant name.+ */+export const langchainDictToolMessageFixture = {+ name: "normalizes a LangChain dict-serialized tool message",+ spanIO: {+ input: [+ {+ content: "It's 60 degrees and foggy.",+ additional_kwargs: {},+ response_metadata: {},+ type: "tool",+ name: "search",+ id: "220ba511-6816-4d1a-8acf-8e5791c2d88e",+ tool_call_id: "call_pNLR4DoZiptb5xGlb299QsoN",+ artifact: null,+ status: "success",+ },+ ],+ output: undefined,+ metadata: undefined,+ },+ expected: {+ messages: [+ {+ id: "220ba511-6816-4d1a-8acf-8e5791c2d88e",+ role: "tool",+ parts: [+ {+ type: "tool-result",+ toolCallId: "call_pNLR4DoZiptb5xGlb299QsoN",+ toolName: "search",+ output: "It's 60 degrees and foggy.",+ },+ ],+ source: "input",+ },+ ],+ toolDefinitions: [],+ },+} satisfies NormalizedIOFixture;++export const langgraphProductionShapeFixture = {+ name: "normalizes an anonymized LangGraph production-shaped span",+ otel: {+ scopeSpan: {+ scope: {+ name: "langfuse-sdk",+ version: "4.14.1",+ attributes: [+ {+ key: "synthetic.metadata.001",+ value: {+ stringValue: "synthetic-value-001",+ },+ },+ ],+ },+ spans: [+ {+ traceId: Buffer.from("00000000000000000000000000000001", "hex"),+ spanId: Buffer.from("0000000000000001", "hex"),+ parentSpanId: Buffer.from("0000000000000002", "hex"),+ name: "synthetic_name_001",+ kind: 1,+ attributes: [+ {+ key: "synthetic.metadata.002",+ value: {+ stringValue: "synthetic-value-002",+ },+ },+ {+ key: "synthetic.metadata.003",+ value: {+ stringValue: "synthetic-value-003",+ },+ },+ {+ key: "langfuse.trace.metadata.participant-id",+ value: {+ stringValue: "synthetic-value-004",+ },+ },+ {+ key: "langfuse.trace.metadata.session-id",+ value: {+ stringValue: "synthetic-value-005",+ },+ },+ {+ key: "langfuse.trace.metadata.ls_integration",+ value: {+ stringValue: "synthetic-value-006",+ },+ },+ {+ key: "langfuse.observation.input",+ value: {+ stringValue:+ '[{"role":"system","content":[{"type":"text","text":"You are a helpful synthetic assistant 007.","synthetic_field_001":{"type":"You are a helpful synthetic assistant 008.","synthetic_field_002":"You are a helpful synthetic assistant 009."}}]},{"role":"user","content":[{"type":"text","text":"Synthetic user request 010."}]},{"role":"assistant","content":[{"text":"Synthetic assistant response 011.","type":"text"},{"id":"call_001","caller":{"type":"direct"},"input":{"synthetic_field_003":"Synthetic assistant response 012."},"name":"synthetic_tool_001","type":"tool_use"},{"id":"call_002","caller":{"type":"direct"},"input":{"synthetic_field_003":"Synthetic assistant response 013."},"name":"synthetic_tool_001","type":"tool_use"}],"tool_calls":[{"name":"synthetic_tool_001","args":{"synthetic_field_003":"Synthetic assistant response 014."},"id":"call_001","type":"tool_call"},{"name":"synthetic_tool_001","args":{"synthetic_field_003":"Synthetic assistant response 015."},"id":"call_002","type":"tool_call"}]},{"role":"tool","content":"synthetic-value-016","tool_call_id":"call_001"},{"role":"tool","content":"synthetic-value-017","tool_call_id":"call_002"},{"role":"tool","content":{"name":"synthetic_tool_001","synthetic_field_004":{"properties":{"synthetic_field_003":{"description":"Synthetic description 018.","type":"string"}},"required":["synthetic_field_003"],"type":"object"},"description":"Synthetic description 019.","synthetic_field_001":{"type":"synthetic-value-020","synthetic_field_002":"synthetic-value-021"}}}]',+ },+ },+ {+ key: "langfuse.observation.model.parameters",+ value: {+ stringValue:+ '{"synthetic_field_005":1,"synthetic_field_006":32000}',+ },+ },+ {+ key: "langfuse.observation.metadata.tags",+ value: {+ stringValue: '["synthetic-value-022"]',+ },+ },+ {+ key: "langfuse.observation.metadata.participant-id",+ value: {+ stringValue: "synthetic-value-023",+ },+ },+ {+ key: "langfuse.observation.metadata.session-id",+ value: {+ stringValue: "synthetic-value-024",+ },+ },+ {+ key: "langfuse.observation.metadata.ls_integration",+ value: {+ stringValue: "synthetic-value-025",+ },+ },+ {+ key: "langfuse.observation.metadata.langgraph_step",+ value: {+ intValue: "synthetic-value-026",+ },+ },+ {+ key: "langfuse.observation.metadata.langgraph_node",+ value: {+ stringValue: "synthetic-value-027",+ },+ },+ {+ key: "langfuse.observation.metadata.langgraph_triggers",+ value: {+ stringValue: '["synthetic-value-028"]',+ },+ },+ {+ key: "langfuse.observation.metadata.langgraph_path",+ value: {+ stringValue: '["synthetic-value-029","synthetic-value-030"]',+ },+ },+ {+ key: "langfuse.observation.metadata.langgraph_checkpoint_ns",+ value: {+ stringValue: "synthetic-value-031",+ },+ },+ {+ key: "langfuse.observation.metadata.checkpoint_ns",+ value: {+ stringValue: "synthetic-value-032",+ },+ },+ {+ key: "langfuse.observation.metadata.ls_provider",+ value: {+ stringValue: "synthetic-value-033",+ },+ },+ {+ key: "langfuse.observation.metadata.ls_model_name",+ value: {+ stringValue: "synthetic-value-034",+ },+ },+ {+ key: "langfuse.observation.metadata.ls_model_type",+ value: {+ stringValue: "synthetic-value-035",+ },+ },+ {+ key: "langfuse.observation.metadata.ls_temperature",+ value: {+ stringValue: "synthetic-value-036",+ },+ },+ {+ key: "langfuse.observation.metadata.ls_max_tokens",+ value: {+ intValue: "synthetic-value-037",+ },+ },+ {+ key: "langfuse.observation.metadata.lc_versions",+ value: {+ stringValue:+ '{"synthetic_field_007":"synthetic-value-038","synthetic_field_008":"synthetic-value-039","synthetic_field_009":"synthetic-value-040"}',+ },+ },+ {+ key: "langfuse.observation.metadata.ocs_provider_type",+ value: {+ stringValue: "synthetic-value-041",+ },+ },+ {+ key: "langfuse.observation.type",+ value: {+ stringValue: "synthetic-value-042",+ },+ },+ {+ key: "langfuse.observation.output",+ value: {+ stringValue:+ '{"role":"assistant","content":"Synthetic assistant response 043."}',+ },+ },+ {+ key: "langfuse.observation.model.name",+ value: {+ stringValue: "synthetic-value-044",+ },+ },+ {+ key: "langfuse.observation.usage_details",+ value: {+ stringValue:+ '{"synthetic_field_010":13252,"synthetic_field_011":0,"input":1,"output":406,"synthetic_field_012":0,"synthetic_field_013":13252}',+ },+ },+ ],+ status: {},+ },+ ],+ },+ resourceAttributes: {+ "telemetry.sdk.language": "python",+ "telemetry.sdk.name": "opentelemetry",+ "telemetry.sdk.version": "1.38.0",+ "service.name": "synthetic-service",+ },+ },+ spanIO: {+ input:+ '[{"role":"system","content":[{"type":"text","text":"You are a helpful synthetic assistant 007.","synthetic_field_001":{"type":"You are a helpful synthetic assistant 008.","synthetic_field_002":"You are a helpful synthetic assistant 009."}}]},{"role":"user","content":[{"type":"text","text":"Synthetic user request 010."}]},{"role":"assistant","content":[{"text":"Synthetic assistant response 011.","type":"text"},{"id":"call_001","caller":{"type":"direct"},"input":{"synthetic_field_003":"Synthetic assistant response 012."},"name":"synthetic_tool_001","type":"tool_use"},{"id":"call_002","caller":{"type":"direct"},"input":{"synthetic_field_003":"Synthetic assistant response 013."},"name":"synthetic_tool_001","type":"tool_use"}],"tool_calls":[{"name":"synthetic_tool_001","args":{"synthetic_field_003":"Synthetic assistant response 014."},"id":"call_001","type":"tool_call"},{"name":"synthetic_tool_001","args":{"synthetic_field_003":"Synthetic assistant response 015."},"id":"call_002","type":"tool_call"}]},{"role":"tool","content":"synthetic-value-016","tool_call_id":"call_001"},{"role":"tool","content":"synthetic-value-017","tool_call_id":"call_002"},{"role":"tool","content":{"name":"synthetic_tool_001","synthetic_field_004":{"properties":{"synthetic_field_003":{"description":"Synthetic description 018.","type":"string"}},"required":["synthetic_field_003"],"type":"object"},"description":"Synthetic description 019.","synthetic_field_001":{"type":"synthetic-value-020","synthetic_field_002":"synthetic-value-021"}}}]',+ output:+ '{"role":"assistant","content":"Synthetic assistant response 043."}',+ metadata: {+ resourceAttributes: {+ "telemetry.sdk.language": "python",+ "telemetry.sdk.name": "opentelemetry",+ "telemetry.sdk.version": "1.38.0",+ "service.name": "synthetic-service",+ },+ scope: {+ name: "langfuse-sdk",+ version: "4.14.1",+ attributes: {+ "synthetic.metadata.001": "synthetic-value-001",+ },+ },+ tags: '["synthetic-value-022"]',+ "participant-id": "synthetic-value-004",+ "session-id": "synthetic-value-005",+ ls_integration: "synthetic-value-006",+ langgraph_step: '{"intValue":"synthetic-value-026"}',+ langgraph_node: "synthetic-value-027",+ langgraph_triggers: '["synthetic-value-028"]',+ langgraph_path: '["synthetic-value-029","synthetic-value-030"]',+ langgraph_checkpoint_ns: "synthetic-value-031",+ checkpoint_ns: "synthetic-value-032",+ ls_provider: "synthetic-value-033",+ ls_model_name: "synthetic-value-034",+ ls_model_type: "synthetic-value-035",+ ls_temperature: "synthetic-value-036",+ ls_max_tokens: '{"intValue":"synthetic-value-037"}',+ lc_versions:+ '{"synthetic_field_007":"synthetic-value-038","synthetic_field_008":"synthetic-value-039","synthetic_field_009":"synthetic-value-040"}',+ ocs_provider_type: "synthetic-value-041",+ },+ },+ expected: {+ messages: [+ {+ role: "system",+ parts: [+ {+ type: "text",+ text: "You are a helpful synthetic assistant 007.",+ providerMetadata: {+ synthetic_field_001: {+ type: "You are a helpful synthetic assistant 008.",+ synthetic_field_002:+ "You are a helpful synthetic assistant 009.",+ },+ },+ },+ ],+ source: "input",+ },+ {+ role: "user",+ parts: [+ {+ type: "text",+ text: "Synthetic user request 010.",+ },+ ],+ source: "input",+ },+ {+ role: "assistant",+ parts: [+ {+ type: "text",+ text: "Synthetic assistant response 011.",+ },+ {+ type: "tool-call",+ toolCallId: "call_001",+ toolName: "synthetic_tool_001",+ input: {+ synthetic_field_003: "Synthetic assistant response 012.",+ },+ toolType: "tool_use",+ providerMetadata: { caller: { type: "direct" } },+ },+ {+ type: "tool-call",+ toolCallId: "call_002",+ toolName: "synthetic_tool_001",+ input: {+ synthetic_field_003: "Synthetic assistant response 013.",+ },+ toolType: "tool_use",+ providerMetadata: { caller: { type: "direct" } },+ },+ ],+ source: "input",+ },+ {+ role: "tool",+ parts: [+ {+ type: "tool-result",+ toolCallId: "call_001",+ output: "synthetic-value-016",+ },+ ],+ source: "input",+ },+ {+ role: "tool",+ parts: [+ {+ type: "tool-result",+ toolCallId: "call_002",+ output: "synthetic-value-017",+ },+ ],+ source: "input",+ },+ {+ role: "tool",+ parts: [+ {+ type: "data",+ value: {+ name: "synthetic_tool_001",+ synthetic_field_004: {+ properties: {+ synthetic_field_003: {+ description: "Synthetic description 018.",+ type: "string",+ },+ },+ required: ["synthetic_field_003"],+ type: "object",+ },+ description: "Synthetic description 019.",+ synthetic_field_001: {+ type: "synthetic-value-020",+ synthetic_field_002: "synthetic-value-021",+ },+ },+ },+ ],+ source: "input",+ },+ {+ role: "assistant",+ parts: [+ {+ type: "text",+ text: "Synthetic assistant response 043.",+ },+ ],+ source: "output",+ },+ ],+ toolDefinitions: [],+ },+} satisfies NormalizedIOFixture;diff --git a/packages/shared/src/utils/normalized-io/conventions/providers/langchain/index.ts b/packages/shared/src/utils/normalized-io/conventions/providers/langchain/index.tsnew file mode 100644index 0000000000..0cd113c33b--- /dev/null+++ b/packages/shared/src/utils/normalized-io/conventions/providers/langchain/index.ts@@ -0,0 +1,126 @@+import { claimed, unmatched } from "../..";+import {+ asRecord,+ compact,+ optionalString,+ parseArray,+} from "../../../core/utils/json";+import { toolCallPart } from "../../../core/normalize/message-parts/tool-calls";+import type {+ NormalizedMessage,+ NormalizedMessagePart,+ ToolCallPart,+} from "../../../types";+import type {+ ConventionResult,+ IOConvention,+ MessageEnvelopeContext,+ PartHandlerContext,+} from "../../io-convention";++/**+ * LangChain / LangGraph convention: this module owns the `lc`/`kwargs`+ * serialization envelope and the `tool_calls`/`invalid_tool_calls`/+ * `additional_kwargs` sibling fields. LangChain has no finish-reason+ * vocabulary of its own β it surfaces the underlying provider's value under+ * `response_metadata` (picked up generically in `normalize/message.ts`).+ */++// FunctionMessage maps to the deprecated "function" role so the legacy+// function-result handling (name as tool name) applies.+const LANGCHAIN_ROLE_BY_CLASS: Record<string, string> = {+ SystemMessage: "system",+ HumanMessage: "user",+ AIMessage: "assistant",+ AIMessageChunk: "assistant",+ ToolMessage: "tool",+ FunctionMessage: "function",+};++/**+ * The LangChain `lc`/`kwargs` serialization envelope: the message fields+ * live in `kwargs`, and the role derives from the class path in `id`.+ */+function unwrapLangchainEnvelope(+ value: Record<string, unknown>,+ fallbackRole: "user" | "assistant",+ ctx: MessageEnvelopeContext,+): ConventionResult<NormalizedMessage> {+ const kwargs = value.lc !== undefined ? asRecord(value.kwargs) : undefined;+ if (!kwargs) return unmatched;++ const classPath = Array.isArray(value.id) ? value.id : [];+ const className = optionalString(classPath[classPath.length - 1]);+ const role = className ? LANGCHAIN_ROLE_BY_CLASS[className] : undefined;+ return claimed(+ ctx.normalizeMessage(+ { ...(role ? { role } : {}), ...kwargs },+ fallbackRole,+ ),+ );+}++/**+ * LangChain's `invalid_tool_calls` (attempts the model made whose arguments+ * could not be parsed β kept in the stream as flagged tool calls, raw args+ * as input, so evals can filter them; excluded from the tool columns) and+ * `additional_kwargs.tool_calls` (an echo of the same `tool_calls` shape).+ */+function langchainCollectSiblingParts(+ value: Record<string, unknown>,+ _baseParts: readonly NormalizedMessagePart[],+ context: PartHandlerContext,+): {+ sourceKey: string;+ slot: "after-tool-calls";+ parts: NormalizedMessagePart[];+}[] {+ const parts: NormalizedMessagePart[] = [];++ const additionalKwargs = asRecord(value.additional_kwargs);+ const additionalToolCalls = context.normalizePartList(+ parseArray(additionalKwargs?.tool_calls) ?? [],+ );+ parts.push(...additionalToolCalls);++ for (const invalidCall of parseArray(value.invalid_tool_calls) ?? []) {+ // LangChain invalid_tool_calls entries: { name, args, id, error }.+ const record = asRecord(invalidCall);+ const part = record+ ? toolCallPart({+ toolCallId: record.id,+ toolName: record.name,+ input: record.args,+ })+ : null;+ if (!part) continue;+ const error = optionalString(record?.error);+ parts.push(+ compact<ToolCallPart>({+ ...part,+ invalid: true,+ providerMetadata: error ? { error } : undefined,+ }),+ );+ }++ return parts.length > 0+ ? [{ sourceKey: "langchain.siblings", slot: "after-tool-calls", parts }]+ : [];+}++export const langchainProvider = {+ name: "langchain",+ // Serialized LangChain message classes carry their role as a type string.+ roleByMessageType: {+ human: "user",+ ai: "assistant",+ tool: "tool",+ system: "system",+ },+ // Parsed calls live in `tool_calls`, raw provider extras in+ // `additional_kwargs`.+ messageLikeKeys: new Set(["tool_calls", "additional_kwargs"]),+ tryUnwrapMessage: unwrapLangchainEnvelope,+ collectSiblingParts: langchainCollectSiblingParts,+} satisfies IOConvention;diff --git a/packages/shared/src/utils/normalized-io/conventions/providers/langchain/langchain.test.ts b/packages/shared/src/utils/normalized-io/conventions/providers/langchain/langchain.test.tsnew file mode 100644index 0000000000..fad6078e8e--- /dev/null+++ b/packages/shared/src/utils/normalized-io/conventions/providers/langchain/langchain.test.ts@@ -0,0 +1,21 @@+import { describe, expect, it } from "vitest";++import { normalizeSpanIO } from "../../../parser";+import {+ langchainDictToolMessageFixture,+ langchainSerializedEnvelopeFixture,+ langgraphProductionShapeFixture,+} from "./fixtures";++describe("LangChain normalized I/O", () => {+ it.each([+ langchainDictToolMessageFixture,+ langchainSerializedEnvelopeFixture,+ langgraphProductionShapeFixture,+ ])("$name", ({ spanIO, expected }) => {+ expect(normalizeSpanIO(spanIO)).toEqual({+ ...expected,+ span: spanIO,+ });+ });+});diff --git a/packages/shared/src/utils/normalized-io/conventions/providers/mixed/fixtures/index.ts b/packages/shared/src/utils/normalized-io/conventions/providers/mixed/fixtures/index.tsnew file mode 100644index 0000000000..054d101f0a--- /dev/null+++ b/packages/shared/src/utils/normalized-io/conventions/providers/mixed/fixtures/index.ts@@ -0,0 +1,11 @@+import { looseProviderMessageShapesFixture } from "./loose-provider-message-shapes";+import { outputOnlyPlainTextFixture } from "./output-only-plain-text";+import { outputOnlyStructuredMessageFixture } from "./output-only-structured-message";+import { rawPassthroughToolCallsFixture } from "./raw-passthrough-tool-calls";++export const mixedNormalizedIOFixtures = [+ looseProviderMessageShapesFixture,+ outputOnlyStructuredMessageFixture,+ outputOnlyPlainTextFixture,+ rawPassthroughToolCallsFixture,+];diff --git a/packages/shared/src/utils/normalized-io/conventions/providers/mixed/fixtures/loose-provider-message-shapes.ts b/packages/shared/src/utils/normalized-io/conventions/providers/mixed/fixtures/loose-provider-message-shapes.tsnew file mode 100644index 0000000000..6f422c6858--- /dev/null+++ b/packages/shared/src/utils/normalized-io/conventions/providers/mixed/fixtures/loose-provider-message-shapes.ts@@ -0,0 +1,110 @@+import type { NormalizedIOFixture } from "../../fixture-types";++/**+ * Loosely structured shapes surfaced by the ChatML-corpus cross-check:+ * - Agno's Python-repr message strings ("role='...' content='...' name=None").+ * - BeeAI's roleless `{text}` message records.+ * - Koog/TraceLoop turns labeled `tool` without a tool_call_id: a JSON-string+ * array of calls is a mislabeled assistant turn; plain text is the tool's+ * response (tool-result), not conversation text.+ * - LangGraph putting the tool name in the role field (falls back to the+ * contextual role, raw string preserved as senderName).+ * - Non-message payloads (function-span args/results) becoming data parts+ * instead of being dropped.+ * - GenAI choice-event envelopes ({index, message, finish_reason}) unwrapping+ * to the nested message.+ */+export const looseProviderMessageShapesFixture = {+ name: "normalizes loosely structured provider message shapes",+ spanIO: {+ input: [+ "role='system' content='Answer politely.' name=None tool_call_id=None",+ {+ text: "What is the weather in Berlin?",+ provider_extension: { confidence: 0.9 },+ },+ {+ role: "tool",+ content:+ '[{"function":{"name":"get_weather","arguments":"{\\"city\\":\\"Berlin\\"}"},"id":"call_loose_1","type":"function"}]',+ },+ { role: "tool", content: "Sunny, 22 degrees" },+ { role: "get_weather", content: "Sunny, 22 degrees (echo)" },+ { unrecognized_payload: { rows: [1, 2] } },+ ],+ output: {+ index: 0,+ message: { role: "assistant", content: "It is sunny in Berlin." },+ finish_reason: "stop",+ "event.name": "gen_ai.choice",+ },+ metadata: undefined,+ },+ expected: {+ messages: [+ {+ role: "system",+ parts: [{ type: "text", text: "Answer politely." }],+ source: "input",+ },+ {+ role: "user",+ parts: [+ {+ type: "text",+ text: "What is the weather in Berlin?",+ providerMetadata: {+ provider_extension: { confidence: 0.9 },+ },+ },+ ],+ source: "input",+ },+ {+ // Mislabeled assistant tool-call turn.+ role: "assistant",+ parts: [+ {+ type: "tool-call",+ toolCallId: "call_loose_1",+ toolName: "get_weather",+ input: { city: "Berlin" },+ toolType: "function",+ },+ ],+ source: "input",+ },+ {+ role: "tool",+ parts: [+ {+ type: "tool-result",+ toolCallId: null,+ output: "Sunny, 22 degrees",+ },+ ],+ source: "input",+ },+ {+ role: "user",+ senderName: "get_weather",+ parts: [{ type: "text", text: "Sunny, 22 degrees (echo)" }],+ source: "input",+ },+ {+ role: "user",+ parts: [+ { type: "data", value: { unrecognized_payload: { rows: [1, 2] } } },+ ],+ source: "input",+ },+ {+ role: "assistant",+ parts: [{ type: "text", text: "It is sunny in Berlin." }],+ finishReason: { type: "stop", raw: "stop" },+ source: "output",+ },+ ],+ toolDefinitions: [],+ },+} satisfies NormalizedIOFixture;diff --git a/packages/shared/src/utils/normalized-io/conventions/providers/mixed/fixtures/output-only-plain-text.ts b/packages/shared/src/utils/normalized-io/conventions/providers/mixed/fixtures/output-only-plain-text.tsnew file mode 100644index 0000000000..f613bea205--- /dev/null+++ b/packages/shared/src/utils/normalized-io/conventions/providers/mixed/fixtures/output-only-plain-text.ts@@ -0,0 +1,20 @@+import type { NormalizedIOFixture } from "../../fixture-types";++export const outputOnlyPlainTextFixture = {+ name: "preserves output-only plain text",+ spanIO: {+ input: undefined,+ output: "plain answer",+ metadata: undefined,+ },+ expected: {+ messages: [+ {+ role: "assistant",+ parts: [{ type: "text", text: "plain answer" }],+ source: "output",+ },+ ],+ toolDefinitions: [],+ },+} satisfies NormalizedIOFixture;diff --git a/packages/shared/src/utils/normalized-io/conventions/providers/mixed/fixtures/output-only-structured-message.ts b/packages/shared/src/utils/normalized-io/conventions/providers/mixed/fixtures/output-only-structured-message.tsnew file mode 100644index 0000000000..9be6a71034--- /dev/null+++ b/packages/shared/src/utils/normalized-io/conventions/providers/mixed/fixtures/output-only-structured-message.ts@@ -0,0 +1,20 @@+import type { NormalizedIOFixture } from "../../fixture-types";++export const outputOnlyStructuredMessageFixture = {+ name: "normalizes an output-only structured assistant message",+ spanIO: {+ input: undefined,+ output: { role: "assistant", content: "final answer" },+ metadata: undefined,+ },+ expected: {+ messages: [+ {+ role: "assistant",+ parts: [{ type: "text", text: "final answer" }],+ source: "output",+ },+ ],+ toolDefinitions: [],+ },+} satisfies NormalizedIOFixture;diff --git a/packages/shared/src/utils/normalized-io/conventions/providers/mixed/fixtures/raw-passthrough-tool-calls.ts b/packages/shared/src/utils/normalized-io/conventions/providers/mixed/fixtures/raw-passthrough-tool-calls.tsnew file mode 100644index 0000000000..d05c055a21--- /dev/null+++ b/packages/shared/src/utils/normalized-io/conventions/providers/mixed/fixtures/raw-passthrough-tool-calls.ts@@ -0,0 +1,73 @@+import type { NormalizedIOFixture } from "../../fixture-types";++/** Synthetic passthrough case adapted from useChatMLParser.clienttest.ts. */+export const rawPassthroughToolCallsFixture = {+ name: "normalizes raw passthrough tool-call aliases",+ spanIO: {+ input: {+ messages: [{ role: "system", content: "Use tools when needed." }],+ tools: [+ { name: "search", parameters: { type: "object" } },+ { name: "lookup", parameters: { type: "object" } },+ ],+ },+ output: {+ tool_calls: [+ {+ id: "call_search_001",+ toolName: "search",+ input: { query: "documentation" },+ },+ {+ id: "call_lookup_001",+ toolName: "lookup",+ args: { identifier: "synthetic-record" },+ },+ ],+ },+ metadata: undefined,+ },+ expected: {+ messages: [+ {+ role: "system",+ parts: [{ type: "text", text: "Use tools when needed." }],+ source: "input",+ },+ {+ role: "assistant",+ parts: [+ {+ type: "tool-call",+ toolCallId: "call_search_001",+ toolName: "search",+ input: { query: "documentation" },+ },+ {+ type: "tool-call",+ toolCallId: "call_lookup_001",+ toolName: "lookup",+ input: { identifier: "synthetic-record" },+ },+ ],+ source: "output",+ },+ ],+ toolDefinitions: [+ {+ name: "search",+ description: undefined,+ inputSchema: { type: "object" },+ type: undefined,+ providerMetadata: undefined,+ },+ {+ name: "lookup",+ description: undefined,+ inputSchema: { type: "object" },+ type: undefined,+ providerMetadata: undefined,+ },+ ],+ },+} satisfies NormalizedIOFixture;diff --git a/packages/shared/src/utils/normalized-io/conventions/providers/mixed/mixed.test.ts b/packages/shared/src/utils/normalized-io/conventions/providers/mixed/mixed.test.tsnew file mode 100644index 0000000000..343ed622c8--- /dev/null+++ b/packages/shared/src/utils/normalized-io/conventions/providers/mixed/mixed.test.ts@@ -0,0 +1,13 @@+import { describe, expect, it } from "vitest";++import { normalizeSpanIO } from "../../../parser";+import { mixedNormalizedIOFixtures } from "./fixtures";++describe("mixed normalized I/O", () => {+ it.each(mixedNormalizedIOFixtures)("$name", ({ spanIO, expected }) => {+ expect(normalizeSpanIO(spanIO)).toEqual({+ ...expected,+ span: spanIO,+ });+ });+});diff --git a/packages/shared/src/utils/normalized-io/conventions/providers/openai/fixtures.ts b/packages/shared/src/utils/normalized-io/conventions/providers/openai/fixtures.tsnew file mode 100644index 0000000000..9f5141dd4f--- /dev/null+++ b/packages/shared/src/utils/normalized-io/conventions/providers/openai/fixtures.ts@@ -0,0 +1,892 @@+import type { NormalizedIOFixture } from "../fixture-types";++const toolCallId = "call_weather_001";+const customToolCallId = "call_custom_002";++/** Synthetic OpenAI chat-completion case adapted from the playground suite. */+export const openAiChatCompletionToolSequenceFixture = {+ name: "normalizes an OpenAI chat-completion tool sequence",+ spanIO: {+ input: {+ tools: [+ {+ type: "function",+ function: {+ name: "get_weather",+ description: "Get the weather for a city",+ parameters: {+ type: "object",+ properties: { city: { type: "string" } },+ required: ["city"],+ },+ },+ },+ {+ type: "custom",+ custom: {+ name: "run_python",+ description: "Runs a python snippet",+ format: { type: "text" },+ },+ },+ ],+ messages: [+ // `name` differentiates same-role participants β carried as the+ // message's senderName (unlike the function-role `name` below,+ // which is a tool name).+ {+ role: "user",+ name: "alice",+ content: "What is the weather in Zurich?",+ },+ {+ role: "assistant",+ content: "",+ tool_calls: [+ {+ id: toolCallId,+ type: "function",+ function: {+ name: "get_weather",+ arguments: '{"city":"Zurich"}',+ },+ },+ {+ id: customToolCallId,+ type: "custom",+ custom: { name: "run_python", input: "print(21 * 2)" },+ },+ ],+ },+ {+ role: "tool",+ tool_call_id: toolCallId,+ content: '{"condition":"sunny","temperature":24}',+ },+ // Deprecated legacy function-calling protocol: the result message+ // carries the function name instead of a tool_call_id.+ { role: "function", name: "run_python", content: "42" },+ ],+ },+ output: {+ role: "assistant",+ content: "It is sunny and 24 degrees in Zurich.",+ },+ metadata: undefined,+ },+ expected: {+ messages: [+ {+ role: "user",+ senderName: "alice",+ parts: [{ type: "text", text: "What is the weather in Zurich?" }],+ source: "input",+ },+ {+ role: "assistant",+ parts: [+ {+ type: "tool-call",+ toolCallId,+ toolName: "get_weather",+ input: { city: "Zurich" },+ toolType: "function",+ },+ {+ type: "tool-call",+ toolCallId: customToolCallId,+ toolName: "run_python",+ input: "print(21 * 2)",+ toolType: "custom",+ },+ ],+ source: "input",+ },+ {+ role: "tool",+ parts: [+ {+ type: "tool-result",+ toolCallId,+ output: { condition: "sunny", temperature: 24 },+ },+ ],+ source: "input",+ },+ {+ role: "tool",+ parts: [+ {+ type: "tool-result",+ toolCallId: null,+ toolName: "run_python",+ output: 42,+ },+ ],+ source: "input",+ },+ {+ role: "assistant",+ parts: [+ { type: "text", text: "It is sunny and 24 degrees in Zurich." },+ ],+ source: "output",+ },+ ],+ toolDefinitions: [+ {+ name: "get_weather",+ description: "Get the weather for a city",+ inputSchema: {+ type: "object",+ properties: { city: { type: "string" } },+ required: ["city"],+ },+ type: "function",+ },+ {+ name: "run_python",+ description: "Runs a python snippet",+ inputSchema: { type: "text" },+ type: "custom",+ },+ ],+ },+} satisfies NormalizedIOFixture;++const callId1 = "call_r1";+const callId2 = "call_r2";++const toolInput1 = { query: "refund policy annual plans" };+const toolInput2 = { query: "annual plan terms 30 day window" };++// Prior turn, replayed as input history: a reasoning item and two parallel+// function calls followed by their outputs. Responses API items carry no+// explicit role β function_call/function_call_output are recognized by+// shape, and standalone reasoning items are model output (assistant) even+// on the input side.+const inputItems = [+ {+ role: "user",+ content:+ "What's our refund policy for annual plans purchased more than 30 days ago?",+ },+ {+ type: "reasoning",+ id: "rs_001",+ summary: [+ {+ type: "summary_text",+ text: "I should check both the refund policy doc and the annual-plan terms doc, since the answer may differ for annual vs monthly plans.",+ },+ ],+ },+ {+ type: "function_call",+ id: "fc_001",+ call_id: callId1,+ name: "search_docs",+ arguments: JSON.stringify(toolInput1),+ status: "completed",+ },+ {+ type: "function_call",+ id: "fc_002",+ call_id: callId2,+ name: "search_docs",+ arguments: JSON.stringify(toolInput2),+ status: "completed",+ },+ {+ type: "function_call_output",+ call_id: callId1,+ output: "[Reduced refund policy doc excerpt]",+ },+ {+ type: "function_call_output",+ call_id: callId2,+ output: "[Reduced annual plan terms excerpt]",+ },+];++// New turn: reasoning followed by a direct answer β the prior parallel+// lookups already covered what was needed, so no new tool call this turn.+const outputItems = [+ {+ type: "reasoning",+ id: "rs_002",+ status: "completed",+ summary: [+ {+ type: "summary_text",+ text: "Both documents indicate refunds aren't available after 30 days for annual plans, so no further lookup is needed.",+ },+ ],+ },+ {+ type: "message",+ id: "msg_001",+ role: "assistant",+ status: "completed",+ content: [+ {+ type: "output_text",+ text: "Refunds for annual plans are only available within the first 30 days of purchase, so a refund isn't possible after that window.",+ },+ ],+ },+];++/**+ * Hand-built from OpenAI's documented Responses API item shapes (not a real+ * capture β the real prod example in this batch was wrapped in LangChain's+ * serialization envelope, which tests LangChain's format rather than the+ * raw Responses API one). Mirrors the same reasoning + parallel-tool-call+ * complexity as anthropicMessagesRichContent for the OpenAI side.+ */+export const openAiResponsesReasoningWithParallelCallsFixture = {+ name: "normalizes OpenAI Responses reasoning with parallel function calls",+ spanIO: {+ input: JSON.stringify(inputItems),+ output: JSON.stringify(outputItems),+ metadata: undefined,+ },+ expected: {+ messages: [+ {+ role: "user",+ parts: [+ {+ type: "text",+ text: "What's our refund policy for annual plans purchased more than 30 days ago?",+ },+ ],+ source: "input",+ },+ {+ id: "rs_001",+ role: "assistant",+ parts: [+ {+ type: "reasoning",+ content: {+ kind: "text",+ text: "I should check both the refund policy doc and the annual-plan terms doc, since the answer may differ for annual vs monthly plans.",+ },+ },+ ],+ source: "input",+ },+ {+ role: "assistant",+ parts: [+ {+ type: "tool-call",+ toolCallId: callId1,+ toolName: "search_docs",+ input: toolInput1,+ toolType: "function_call",+ providerMetadata: { status: "completed" },+ },+ {+ type: "tool-call",+ toolCallId: callId2,+ toolName: "search_docs",+ input: toolInput2,+ toolType: "function_call",+ providerMetadata: { status: "completed" },+ },+ ],+ source: "input",+ },+ {+ role: "tool",+ parts: [+ {+ type: "tool-result",+ toolCallId: callId1,+ output: "[Reduced refund policy doc excerpt]",+ },+ ],+ source: "input",+ },+ {+ role: "tool",+ parts: [+ {+ type: "tool-result",+ toolCallId: callId2,+ output: "[Reduced annual plan terms excerpt]",+ },+ ],+ source: "input",+ },+ {+ id: "rs_002",+ role: "assistant",+ parts: [+ {+ type: "reasoning",+ content: {+ kind: "text",+ text: "Both documents indicate refunds aren't available after 30 days for annual plans, so no further lookup is needed.",+ },+ },+ ],+ source: "output",+ },+ {+ id: "msg_001",+ role: "assistant",+ parts: [+ {+ type: "text",+ text: "Refunds for annual plans are only available within the first 30 days of purchase, so a refund isn't possible after that window.",+ },+ ],+ source: "output",+ },+ ],+ toolDefinitions: [],+ },+} satisfies NormalizedIOFixture;++const responsesToolCallId = "call_weather_002";++/** Synthetic OpenAI Responses case adapted from the playground suite. */+export const openAiResponsesFunctionCallFixture = {+ name: "normalizes OpenAI Responses function calls and outputs",+ spanIO: {+ input: [+ { role: "user", content: "What is the weather in Basel?" },+ {+ type: "function_call",+ id: "fc_weather_002",+ call_id: responsesToolCallId,+ name: "get_weather",+ arguments: { city: "Basel" },+ status: "completed",+ },+ {+ type: "function_call_output",+ call_id: responsesToolCallId,+ output: "The weather in Basel is cloudy.",+ },+ ],+ output: undefined,+ metadata: undefined,+ },+ expected: {+ messages: [+ {+ role: "user",+ parts: [{ type: "text", text: "What is the weather in Basel?" }],+ source: "input",+ },+ {+ role: "assistant",+ parts: [+ {+ type: "tool-call",+ toolCallId: responsesToolCallId,+ toolName: "get_weather",+ input: { city: "Basel" },+ toolType: "function_call",+ providerMetadata: { status: "completed" },+ },+ ],+ source: "input",+ },+ {+ role: "tool",+ parts: [+ {+ type: "tool-result",+ toolCallId: responsesToolCallId,+ output: "The weather in Basel is cloudy.",+ },+ ],+ source: "input",+ },+ ],+ toolDefinitions: [],+ },+} satisfies NormalizedIOFixture;++/**+ * Synthetic OpenAI chat-completion case for the non-text surfaces: multimodal+ * content parts (image_url / input_audio / file in all three source shapes β+ * https URL, base64 data-URI, and Langfuse media reference token), refusal+ * parts, response-message url_citation annotations, and audio output.+ *+ * Media handling contract exercised here:+ * - `@@@langfuseMedia:type=X|id=Y|source=Z@@@` tokens (the dominant shape in+ * stored production IO) become `file` parts with `kind: "reference"`,+ * mediaType from the token, and the token's `source` in providerMetadata.+ * - Unknown media subtypes fall back to modality wildcards (`image/*`,+ * `audio/*`) when the part kind reveals the modality, and omit mediaType+ * entirely when it does not (opaque file ids).+ * - Refusals stay findable: they normalize to text parts flagged with+ * `refusal: true` (typed field) so evals can filter refusal observations.+ */+export const openAiChatMultimodalRichResponseFixture = {+ name: "normalizes OpenAI chat-completion multimodal content and rich response fields",+ spanIO: {+ input: {+ messages: [+ // `developer` is OpenAI's replacement name for `system` β aliases to+ // the canonical system role.+ { role: "developer", content: "You are a helpful assistant." },+ {+ role: "user",+ content: [+ { type: "text", text: "What is in these files?" },+ {+ type: "image_url",+ image_url: { url: "https://example.com/cat.png", detail: "low" },+ },+ {+ type: "image_url",+ image_url: { url: "data:image/png;base64,aGVsbG8=" },+ },+ {+ type: "image_url",+ image_url: {+ url: "@@@langfuseMedia:type=image/jpeg|id=media-ref-image-1|source=base64@@@",+ },+ },+ {+ type: "input_audio",+ input_audio: { data: "UklGRg==", format: "wav" },+ },+ {+ type: "input_audio",+ input_audio: {+ data: "@@@langfuseMedia:type=audio/mpeg|id=media-ref-audio-1|source=base64@@@",+ },+ },+ {+ type: "file",+ file: { file_data: "JVBERi0=", filename: "report.pdf" },+ },+ { type: "file", file: { file_id: "file-abc123" } },+ "@@@langfuseMedia:type=application/pdf|id=media-ref-file-1|source=bytes@@@",+ // Text with several embedded media tokens splits into+ // interleaved text and file parts.+ {+ type: "text",+ text: "Compare @@@langfuseMedia:type=image/png|id=media-ref-inline-1|source=base64@@@ with @@@langfuseMedia:type=image/png|id=media-ref-inline-2|source=base64@@@ please.",+ },+ ],+ },+ {+ role: "assistant",+ content: [+ { type: "refusal", refusal: "I cannot describe this image." },+ ],+ },+ ],+ },+ output: {+ choices: [+ {+ index: 0,+ message: {+ role: "assistant",+ content: "The Eiffel Tower is 330 meters tall.",+ refusal: null,+ annotations: [+ {+ type: "url_citation",+ url_citation: {+ url: "https://example.com/eiffel",+ title: "Eiffel Tower",+ start_index: 0,+ end_index: 37,+ },+ },+ ],+ audio: {+ id: "audio_001",+ data: "@@@langfuseMedia:type=audio/mpeg|id=media-ref-audio-2|source=base64@@@",+ transcript: "The Eiffel Tower is 330 meters tall.",+ expires_at: 1755672000,+ },+ },+ finish_reason: "stop",+ },+ ],+ },+ metadata: undefined,+ },+ expected: {+ messages: [+ {+ role: "system",+ parts: [{ type: "text", text: "You are a helpful assistant." }],+ source: "input",+ },+ {+ role: "user",+ parts: [+ { type: "text", text: "What is in these files?" },+ {+ type: "file",+ mediaType: "image/*",+ content: { kind: "url", url: "https://example.com/cat.png" },+ providerMetadata: { detail: "low" },+ },+ {+ type: "file",+ // Data-URIs stay urls (they render as urls; decoding is the media+ // pipeline's job), but the prefix still declares the exact type.+ mediaType: "image/png",+ content: { kind: "url", url: "data:image/png;base64,aGVsbG8=" },+ },+ {+ type: "file",+ mediaType: "image/jpeg",+ content: { kind: "reference", id: "media-ref-image-1" },+ providerMetadata: { source: "base64" },+ },+ {+ type: "file",+ mediaType: "audio/wav",+ content: { kind: "base64", data: "UklGRg==" },+ },+ {+ type: "file",+ mediaType: "audio/mpeg",+ content: { kind: "reference", id: "media-ref-audio-1" },+ providerMetadata: { source: "base64" },+ },+ {+ type: "file",+ filename: "report.pdf",+ content: { kind: "base64", data: "JVBERi0=" },+ },+ {+ type: "file",+ content: { kind: "reference", id: "file-abc123" },+ },+ {+ type: "file",+ mediaType: "application/pdf",+ content: { kind: "reference", id: "media-ref-file-1" },+ providerMetadata: { source: "bytes" },+ },+ { type: "text", text: "Compare " },+ {+ type: "file",+ mediaType: "image/png",+ content: { kind: "reference", id: "media-ref-inline-1" },+ providerMetadata: { source: "base64" },+ },+ { type: "text", text: " with " },+ {+ type: "file",+ mediaType: "image/png",+ content: { kind: "reference", id: "media-ref-inline-2" },+ providerMetadata: { source: "base64" },+ },+ { type: "text", text: " please." },+ ],+ source: "input",+ },+ {+ role: "assistant",+ parts: [+ {+ type: "text",+ refusal: true,+ text: "I cannot describe this image.",+ },+ ],+ source: "input",+ },+ {+ role: "assistant",+ parts: [+ {+ type: "text",+ text: "The Eiffel Tower is 330 meters tall.",+ providerMetadata: {+ citations: [+ {+ type: "url_citation",+ url_citation: {+ url: "https://example.com/eiffel",+ title: "Eiffel Tower",+ start_index: 0,+ end_index: 37,+ },+ },+ ],+ },+ },+ {+ type: "file",+ mediaType: "audio/mpeg",+ content: { kind: "reference", id: "media-ref-audio-2" },+ providerMetadata: {+ source: "base64",+ id: "audio_001",+ transcript: "The Eiffel Tower is 330 meters tall.",+ expires_at: 1755672000,+ },+ },+ ],+ // The finish reason lives on the choice, not the message.+ finishReason: { type: "stop", raw: "stop" },+ source: "output",+ },+ ],+ toolDefinitions: [],+ },+} satisfies NormalizedIOFixture;++/**+ * Hand-built from OpenAI's documented Responses API item shapes, covering the+ * non-function surfaces: media inputs (input_image / input_file), a reasoning+ * item with both raw content (reasoning_text) and summary plus+ * encrypted_content, provider-executed built-in tool items (web_search_call,+ * mcp_call), the custom tool-call protocol, and per-part citation+ * annotations with a refusal in the output message.+ *+ * Built-in items carry no name/arguments β the item type is the tool. They+ * normalize to provider-executed tool calls with the raw item type as+ * toolType and the kind-specific payload as input.+ */+export const openAiResponsesBuiltInToolsAndMediaFixture = {+ name: "normalizes OpenAI Responses built-in tools, media inputs, and rich reasoning",+ spanIO: {+ input: JSON.stringify([+ {+ role: "user",+ content: [+ {+ type: "input_text",+ text: "Check the docs and describe the diagram.",+ },+ {+ type: "input_image",+ detail: "auto",+ image_url: "https://example.com/diagram.png",+ },+ {+ type: "input_file",+ file_url: "https://example.com/spec.pdf",+ filename: "spec.pdf",+ },+ ],+ },+ ]),+ output: JSON.stringify([+ {+ type: "reasoning",+ id: "rs_100",+ summary: [{ type: "summary_text", text: "Weighing the sources." }],+ content: [+ { type: "reasoning_text", text: "Detailed chain of thought." },+ ],+ encrypted_content: "enc_abc123",+ },+ {+ type: "web_search_call",+ id: "ws_100",+ status: "completed",+ action: { type: "search", query: "diagram spec" },+ },+ {+ type: "custom_tool_call",+ id: "ctc_100",+ call_id: "call_custom_100",+ name: "render_diagram",+ input: "graph TD; A-->B",+ },+ {+ type: "custom_tool_call_output",+ id: "cto_100",+ call_id: "call_custom_100",+ output: "ok",+ },+ {+ type: "mcp_call",+ id: "mcp_100",+ name: "query_db",+ arguments: '{"sql":"SELECT 1"}',+ server_label: "analytics",+ output: '[{"one":1}]',+ },+ {+ type: "mcp_list_tools",+ id: "mcpl_100",+ server_label: "analytics",+ tools: [+ {+ name: "query_db",+ description: "Run a SQL query",+ input_schema: { type: "object" },+ },+ ],+ },+ {+ type: "message",+ id: "msg_100",+ role: "assistant",+ status: "completed",+ content: [+ {+ type: "output_text",+ text: "The diagram shows a simple flow.",+ annotations: [+ {+ type: "url_citation",+ url_citation: {+ url: "https://example.com/spec",+ title: "Spec",+ start_index: 0,+ end_index: 32,+ },+ },+ ],+ },+ { type: "refusal", refusal: "I cannot share the internal spec." },+ ],+ },+ ]),+ metadata: undefined,+ },+ expected: {+ messages: [+ {+ role: "user",+ parts: [+ { type: "text", text: "Check the docs and describe the diagram." },+ {+ type: "file",+ mediaType: "image/*",+ content: { kind: "url", url: "https://example.com/diagram.png" },+ providerMetadata: { detail: "auto" },+ },+ {+ type: "file",+ filename: "spec.pdf",+ content: { kind: "url", url: "https://example.com/spec.pdf" },+ },+ ],+ source: "input",+ },+ {+ id: "rs_100",+ role: "assistant",+ parts: [+ {+ type: "reasoning",+ content: { kind: "text", text: "Detailed chain of thought." },+ },+ {+ type: "reasoning",+ content: { kind: "text", text: "Weighing the sources." },+ },+ // The replayable encrypted blob is its own stream element.+ {+ type: "reasoning",+ content: { kind: "encrypted", data: "enc_abc123" },+ },+ ],+ source: "output",+ },+ {+ role: "assistant",+ parts: [+ {+ type: "tool-call",+ toolCallId: "ws_100",+ toolName: "web_search",+ input: { action: { type: "search", query: "diagram spec" } },+ toolType: "web_search_call",+ providerExecuted: true,+ providerMetadata: { status: "completed" },+ },+ {+ type: "tool-call",+ toolCallId: "call_custom_100",+ toolName: "render_diagram",+ input: "graph TD; A-->B",+ toolType: "custom",+ },+ ],+ source: "output",+ },+ {+ role: "tool",+ parts: [+ {+ type: "tool-result",+ toolCallId: "call_custom_100",+ output: "ok",+ },+ ],+ source: "output",+ },+ {+ role: "assistant",+ parts: [+ {+ type: "tool-call",+ toolCallId: "mcp_100",+ toolName: "query_db",+ input: { sql: "SELECT 1" },+ toolType: "mcp_call",+ providerExecuted: true,+ providerMetadata: {+ server_label: "analytics",+ output: '[{"one":1}]',+ },+ },+ ],+ source: "output",+ },+ {+ id: "msg_100",+ role: "assistant",+ parts: [+ {+ type: "text",+ text: "The diagram shows a simple flow.",+ providerMetadata: {+ citations: [+ {+ type: "url_citation",+ url_citation: {+ url: "https://example.com/spec",+ title: "Spec",+ start_index: 0,+ end_index: 32,+ },+ },+ ],+ },+ },+ {+ type: "text",+ refusal: true,+ text: "I cannot share the internal spec.",+ },+ ],+ source: "output",+ },+ ],+ toolDefinitions: [+ {+ // From the mcp_list_tools item β definitions, not conversation+ // content; no message is emitted for the listing itself.+ name: "query_db",+ description: "Run a SQL query",+ inputSchema: { type: "object" },+ type: undefined,+ },+ ],+ },+} satisfies NormalizedIOFixture;diff --git a/packages/shared/src/utils/normalized-io/conventions/providers/openai/index.ts b/packages/shared/src/utils/normalized-io/conventions/providers/openai/index.tsnew file mode 100644index 0000000000..80230b5525--- /dev/null+++ b/packages/shared/src/utils/normalized-io/conventions/providers/openai/index.ts@@ -0,0 +1,563 @@+import { claimed, unmatched } from "../..";+import {+ asRecord,+ compact,+ nullableString,+ omitKeys,+ optionalString,+ parseArray,+ parseIfString,+ toJsonValue,+ toProviderMetadata,+} from "../../../core/utils/json";+import {+ filePartFromMediaReference,+ filePartFromUrl,+ parseMediaReference,+} from "../../../core/normalize/message-parts/media";+import { reasoningPart } from "../../../core/normalize/message-parts/reasoning";+import {+ providerExecutedToolCall,+ toolCallPart,+} from "../../../core/normalize/message-parts/tool-calls";+import { toolResultPart } from "../../../core/normalize/message-parts/tool-results";+import {+ toolDefinition,+ toolDefinitionProviderMetadata,+} from "../../../core/normalize/tool-definitions";+import type {+ FilePart,+ FinishReason,+ NormalizedMessage,+ NormalizedMessagePart,+ ToolCallPart,+} from "../../../types";+import type {+ ConventionResult,+ IOConvention,+ MessageEnvelopeContext,+ PartHandler,+ MessageSource,+ SiblingPartContribution,+} from "../../io-convention";++/**+ * OpenAI convention: Chat Completions and Responses API shapes. This module+ * owns OpenAI's typed block vocabulary, its `choices`/`output` response+ * envelopes, and the `refusal`/`audio`/reasoning-item sibling fields.+ */++// OpenAI Chat Completions `finish_reason` vocabulary -> the canonical+// FinishReason set.+const OPENAI_FINISH_REASON_TYPE_BY_RAW: Record<string, FinishReason["type"]> = {+ stop: "stop",+ length: "length",+ tool_calls: "tool-calls",+ function_call: "tool-calls",+ content_filter: "content-filter",+};++/**+ * OpenAI Responses built-in (provider-executed) tool items. They carry no+ * `name`/`arguments`; the item type is the tool. Kind-specific payloads+ * (action, queries, code, results, ...) travel in `input` unsplit β the API+ * reports request and result on the same item.+ */+const RESPONSES_BUILT_IN_TOOL_ITEM_TYPES = [+ "web_search_call",+ "file_search_call",+ "code_interpreter_call",+ "computer_call",+ "image_generation_call",+ "local_shell_call",+ "shell_call",+ "apply_patch_call",+ "tool_search_call",+];++function normalizeBuiltInToolItem(+ value: Record<string, unknown>,+): ToolCallPart | null {+ const type = typeof value.type === "string" ? value.type : undefined;+ if (!type) return null;++ if (type === "mcp_call") {+ // MCP calls are real named calls that happen to be provider-executed;+ // server_label/output/error ride in providerMetadata.+ return providerExecutedToolCall(+ toolCallPart({+ toolCallId: value.call_id ?? value.id,+ toolName: value.name,+ input: value.arguments,+ toolType: type,+ }),+ omitKeys(value, ["id", "call_id", "type", "name", "arguments", "status"]),+ );+ }++ const status = optionalString(value.status);+ return compact<ToolCallPart>({+ type: "tool-call",+ toolCallId: nullableString(value.call_id ?? value.id),+ toolName: type.replace(/_call$/, ""),+ input: toJsonValue(omitKeys(value, ["id", "call_id", "type", "status"])),+ toolType: type,+ providerExecuted: true,+ providerMetadata: status ? { status } : undefined,+ });+}++// Chat Completions tool_calls entries ({ id, index?, function: { name,+// arguments } }) and Responses/legacy flat items ({ call_id | id, name,+// arguments }) share these two type names.+const normalizeOpenAiFunctionCall: PartHandler = (value) => {+ const functionCall = asRecord(value.function);+ return claimed(+ toolCallPart({+ toolCallId: value.call_id ?? value.id,+ toolName: value.name ?? functionCall?.name,+ input: value.arguments ?? functionCall?.arguments,+ toolType:+ optionalString(value.type) ?? (functionCall ? "function" : undefined),+ }),+ );+};++const normalizeOpenAiCustomToolCall: PartHandler = (value) => {+ // OpenAI custom tool call: { id, type: "custom", custom: { name, input } }.+ const custom = asRecord(value.custom);+ if (!custom || !optionalString(custom.name) || !("input" in custom)) {+ return unmatched;+ }+ return claimed(+ toolCallPart({+ toolCallId: value.id,+ toolName: custom.name,+ input: custom.input,+ toolType: "custom",+ }),+ );+};++const normalizeOpenAiImageUrl: PartHandler = (value) => {+ const image = asRecord(value.image_url);+ const url = optionalString(image?.url);+ if (!url) return unmatched;+ const detail = optionalString(image?.detail);+ const part = filePartFromUrl(url, {+ fallbackMediaType: "image/*",+ extras: detail ? { detail } : undefined,+ });+ return claimed(part);+};++const normalizeOpenAiInputImage: PartHandler = (value) => {+ // OpenAI Responses image: flat fields instead of the chat wrapper.+ const detail = optionalString(value.detail);+ const extras = detail ? { detail } : undefined;+ const url = optionalString(value.image_url);+ if (url) {+ return claimed(+ filePartFromUrl(url, { fallbackMediaType: "image/*", extras }),+ );+ }+ const fileId = optionalString(value.file_id);+ if (!fileId) return unmatched;+ return claimed(+ compact<FilePart>({+ type: "file",+ mediaType: "image/*",+ content: { kind: "reference", id: fileId },+ providerMetadata: extras ? toProviderMetadata(extras) : undefined,+ }),+ );+};++const normalizeOpenAiInputAudio: PartHandler = (value) => {+ const audio = asRecord(value.input_audio);+ const data = optionalString(audio?.data);+ if (!data) return unmatched;+ const reference = parseMediaReference(data);+ if (reference) return claimed(filePartFromMediaReference(reference));+ const format = optionalString(audio?.format);+ return claimed({+ type: "file",+ mediaType: format ? `audio/${format}` : "audio/*",+ content: { kind: "base64", data },+ });+};++/**+ * OpenAI file fields (`file` content parts and Responses `input_file` items+ * share them): base64 `file_data` (possibly a media token), a `file_url`, or+ * an opaque `file_id` reference, plus an optional `filename`.+ */+function filePartFromFileFields(+ fields: Record<string, unknown>,+): FilePart | null {+ const filename = optionalString(fields.filename);+ const fileData = optionalString(fields.file_data);+ const reference = parseMediaReference(fileData);+ if (reference) {+ return compact({ ...filePartFromMediaReference(reference), filename });+ }++ const fileUrl = optionalString(fields.file_url);+ const fileId = optionalString(fields.file_id);+ const content: FilePart["content"] | undefined = fileData+ ? { kind: "base64", data: fileData }+ : fileUrl+ ? { kind: "url", url: fileUrl }+ : fileId+ ? { kind: "reference", id: fileId }+ : undefined;+ if (!content) return null;++ return compact<FilePart>({ type: "file", filename, content });+}++const normalizeOpenAiFile: PartHandler = (value) => {+ // OpenAI chat `file` content parts wrap the fields under `file`; flat+ // `data`/`url` payloads on the same type name belong to the AI SDK+ // dialect and fall through.+ const file = asRecord(value.file);+ if (!file) return unmatched;+ const part = filePartFromFileFields(file);+ return part ? claimed(part) : unmatched;+};++const normalizeOpenAiInputFile: PartHandler = (value) => {+ // OpenAI Responses file: same fields as the chat `file` wrapper, flat.+ const part = filePartFromFileFields(value);+ return part ? claimed(part) : unmatched;+};++const normalizeOpenAiRefusal: PartHandler = (value) => {+ // Refusal text stays part of the conversation stream.+ const refusal = optionalString(value.refusal);+ return refusal+ ? claimed({ type: "text", refusal: true, text: refusal })+ : unmatched;+};++const normalizeOpenAiMcpCall: PartHandler = (value) => {+ const part = normalizeBuiltInToolItem(value);+ return part ? claimed(part) : unmatched;+};++const normalizeOpenAiBuiltInCall: PartHandler = (value) =>+ claimed(normalizeBuiltInToolItem(value));++// OpenAI Responses output items: { type: "*_output", call_id, output }.+const normalizeOpenAiToolResult: PartHandler = (value) =>+ claimed(+ toolResultPart({+ toolCallId: value.call_id ?? value.id,+ output: value.output,+ }),+ );++// OpenAI Responses custom (free-form input) tool call:+// { type: "custom_tool_call", call_id, name, input }.+const normalizeOpenAiResponsesCustomToolCall: PartHandler = (value) =>+ claimed(+ toolCallPart({+ toolCallId: value.call_id ?? value.id,+ toolName: value.name,+ input: value.input ?? value.arguments,+ toolType: "custom",+ }),+ );++// OpenAI Responses reasoning content/summary entries: { type, text }.+const normalizeOpenAiReasoningText: PartHandler = (value) =>+ claimed(reasoningPart(value.text));++const OPENAI_PART_HANDLERS = {+ function: normalizeOpenAiFunctionCall,+ function_call: normalizeOpenAiFunctionCall,+ custom_tool_call: normalizeOpenAiResponsesCustomToolCall,+ custom: normalizeOpenAiCustomToolCall,+ image_url: normalizeOpenAiImageUrl,+ input_image: normalizeOpenAiInputImage,+ input_audio: normalizeOpenAiInputAudio,+ file: normalizeOpenAiFile,+ input_file: normalizeOpenAiInputFile,+ refusal: normalizeOpenAiRefusal,+ reasoning_text: normalizeOpenAiReasoningText,+ summary_text: normalizeOpenAiReasoningText,+ mcp_call: normalizeOpenAiMcpCall,+ ...Object.fromEntries(+ RESPONSES_BUILT_IN_TOOL_ITEM_TYPES.map((type) => [+ type,+ normalizeOpenAiBuiltInCall,+ ]),+ ),+ function_call_output: normalizeOpenAiToolResult,+ custom_tool_call_output: normalizeOpenAiToolResult,+ computer_call_output: normalizeOpenAiToolResult,+ local_shell_call_output: normalizeOpenAiToolResult,+} satisfies Readonly<Record<string, PartHandler>>;++/**+ * OpenAI audio output (`message.audio`): `{ id, data, transcript, expires_at }`+ * on response messages, `{ id }` as the request-side reference. The playable+ * payload becomes the file part; transcript and the remaining fields ride+ * along in providerMetadata so the stream stays renderable media-first.+ */+function normalizeAudioOutput(+ audio: Record<string, unknown> | undefined,+): FilePart | null {+ if (!audio) return null;++ const { data, ...extras } = audio;+ const payload = optionalString(data);+ const reference = parseMediaReference(payload);+ if (reference) return filePartFromMediaReference(reference, extras);++ if (payload) {+ return compact<FilePart>({+ type: "file",+ mediaType: "audio/*",+ content: { kind: "base64", data: payload },+ providerMetadata: toProviderMetadata(extras),+ });+ }++ const id = optionalString(audio.id);+ if (!id) return null;+ return compact<FilePart>({+ type: "file",+ mediaType: "audio/*",+ content: { kind: "reference", id },+ providerMetadata: toProviderMetadata(omitKeys(extras, ["id"])),+ });+}++/**+ * OpenAI message-sibling fields: `refusal`/`audio`, and Responses reasoning+ * items (content[] is collected via the regular parts path; `summary` is a+ * sibling stream collected either way, and the replayable `encrypted_content`+ * blob becomes its own stream element after the visible parts).+ */+function openAiCollectSiblingParts(+ value: Record<string, unknown>,+ baseParts: readonly NormalizedMessagePart[],+ context: { normalizePartList(values: unknown[]): NormalizedMessagePart[] },+): SiblingPartContribution[] {+ const parts: NormalizedMessagePart[] = [];++ const refusal = optionalString(value.refusal);+ if (refusal) parts.push({ type: "text", refusal: true, text: refusal });++ const audioPart = normalizeAudioOutput(asRecord(value.audio));+ if (audioPart) parts.push(audioPart);++ if (value.type === "reasoning") {+ const reasoningValues = (+ baseParts.length === 0 ? [value.summary, value.content] : [value.summary]+ )+ .flatMap((entry) => (Array.isArray(entry) ? entry : [entry]))+ .filter((entry) => entry !== undefined && entry !== null);+ parts.push(...context.normalizePartList(reasoningValues));++ const encryptedContent = optionalString(value.encrypted_content);+ if (encryptedContent) {+ parts.push({+ type: "reasoning",+ content: { kind: "encrypted", data: encryptedContent },+ });+ }+ }++ return parts.length > 0+ ? [{ sourceKey: "openai.siblings", slot: "after-tool-calls", parts }]+ : [];+}++/**+ * OpenAI response envelopes: Chat Completions `choices[]` (finish reason+ * per choice) and Responses' `output[]` item array.+ */+function openAiMessages(+ root: Record<string, unknown>,+ kind: "input" | "output",+): MessageSource[] {+ if (kind !== "output") return [];++ const choices = parseArray(root.choices);+ if (choices) {+ const sources: MessageSource[] = [];+ for (const choice of choices) {+ const choiceRecord = asRecord(choice);+ sources.push({+ kind: "single",+ value: choiceRecord?.message,+ fallbackRole: "assistant",+ // Chat Completions reports the finish reason on the choice, not the+ // message.+ finishReasonCarrier: choiceRecord,+ });+ }+ return sources;+ }++ const responseOutput = parseArray(root.output);+ if (+ responseOutput &&+ responseOutput.some((item) => {+ const record = asRecord(item);+ return Boolean(+ record?.type &&+ (String(record.type).includes("call") ||+ String(record.type).endsWith("_output") ||+ record.type === "message" ||+ record.type === "reasoning"),+ );+ })+ ) {+ return [+ {+ kind: "sequence",+ values: responseOutput,+ fallbackRole: "assistant",+ },+ ];+ }++ return [];+}++// OpenAI carries citations as `annotations` β per part on Responses+// output_text, message-level on Chat Completions.+const OPENAI_CITATION_KEYS = new Set(["annotations"]);++// Chat Completions carries assistant calls in a `tool_calls` sibling array.+const OPENAI_MESSAGE_LIKE_KEYS = new Set(["tool_calls"]);++/**+ * OpenAI tool declarations: function tools wrap their fields under+ * `function`; custom (free-form input) tools wrap under `custom` or declare+ * flat with `format` as the input schema (Responses).+ */+const tryNormalizeOpenAiToolDefinition = (value: Record<string, unknown>) => {+ const functionDefinition = asRecord(value.function);+ if (functionDefinition) {+ const definition = toolDefinition({+ name: functionDefinition.name ?? value.name,+ description: functionDefinition.description,+ inputSchema: functionDefinition.parameters,+ type: value.type,+ providerMetadata: toolDefinitionProviderMetadata(+ value,+ functionDefinition,+ ),+ });+ return definition ? claimed(definition) : unmatched;+ }++ const customDefinition = asRecord(value.custom);+ if (customDefinition) {+ const definition = toolDefinition({+ name: customDefinition.name ?? value.name,+ description: customDefinition.description,+ inputSchema: customDefinition.format,+ type: value.type,+ providerMetadata: toolDefinitionProviderMetadata(value, customDefinition),+ });+ return definition ? claimed(definition) : unmatched;+ }++ if (value.format !== undefined) {+ const definition = toolDefinition({+ name: value.name,+ description: value.description,+ inputSchema: value.format,+ type: value.type,+ providerMetadata: toolDefinitionProviderMetadata(value, value),+ });+ return definition ? claimed(definition) : unmatched;+ }++ return unmatched;+};++/**+ * Deprecated function-calling protocol: `{role: "function", name, content}`+ * is a tool result carrying the function name instead of a tool_call_id.+ * Claimed as a whole message so `name` becomes the tool name rather than a+ * participant name.+ */+const tryUnwrapLegacyFunctionMessage = (+ value: Record<string, unknown>,+ _fallbackRole: "user" | "assistant",+ ctx: MessageEnvelopeContext,+): ConventionResult<NormalizedMessage> => {+ if (+ // LangChain dict serializations carry `type: "tool"` instead of a role+ // key; the same tool-result shape (tool_call_id, artifact, status).+ (value.role === "tool" || value.type === "tool") &&+ (typeof value.tool_call_id === "string" ||+ typeof value.tool_call_id === "number")+ ) {+ return claimed({+ ...(optionalString(value.id) ? { id: String(value.id) } : {}),+ role: "tool",+ parts: [+ compact({+ type: "tool-result",+ toolCallId: nullableString(value.tool_call_id),+ // A `name` on a tool message is the tool's name, never a+ // participant name.+ toolName: optionalString(value.name),+ output: toJsonValue(parseIfString(value.content ?? null)),+ isError: value.status === "error" ? true : undefined,+ providerMetadata:+ value.artifact !== undefined && value.artifact !== null+ ? toProviderMetadata({ artifact: value.artifact })+ : undefined,+ }),+ ],+ source: ctx.source,+ });+ }++ if (+ typeof value.role !== "string" ||+ value.role.toLowerCase() !== "function"+ ) {+ return unmatched;+ }++ return claimed({+ ...(optionalString(value.id) ? { id: String(value.id) } : {}),+ role: "tool",+ parts: [+ toolResultPart({+ toolCallId: value.tool_call_id,+ toolName: value.name,+ output: value.content,+ }),+ ],+ source: ctx.source,+ });+};++export const openAiProvider: IOConvention = {+ name: "openai",+ finishReasonTypeByRaw: OPENAI_FINISH_REASON_TYPE_BY_RAW,+ tryUnwrapMessage: tryUnwrapLegacyFunctionMessage,+ tryNormalizeToolDefinition: tryNormalizeOpenAiToolDefinition,+ // Deprecated function-calling protocol: function messages are tool results.+ // `developer` is OpenAI's replacement name for `system` (o1+); the API+ // treats them as aliases, so the canonical vocabulary keeps only `system`.+ roleByRawRole: { function: "tool", developer: "system" },+ // Responses reasoning items carry no role but are model output even when+ // replayed on the input side.+ roleByMessageType: { reasoning: "assistant" },+ messageLikeKeys: OPENAI_MESSAGE_LIKE_KEYS,+ citationKeys: OPENAI_CITATION_KEYS,+ typedParts: OPENAI_PART_HANDLERS,+ collectSiblingParts: openAiCollectSiblingParts,+ claimMessages: openAiMessages,+};diff --git a/packages/shared/src/utils/normalized-io/conventions/providers/openai/openai.test.ts b/packages/shared/src/utils/normalized-io/conventions/providers/openai/openai.test.tsnew file mode 100644index 0000000000..ed2a0ab63b--- /dev/null+++ b/packages/shared/src/utils/normalized-io/conventions/providers/openai/openai.test.ts@@ -0,0 +1,25 @@+import { describe, expect, it } from "vitest";++import { normalizeSpanIO } from "../../../parser";+import {+ openAiChatCompletionToolSequenceFixture,+ openAiChatMultimodalRichResponseFixture,+ openAiResponsesBuiltInToolsAndMediaFixture,+ openAiResponsesFunctionCallFixture,+ openAiResponsesReasoningWithParallelCallsFixture,+} from "./fixtures";++describe("OpenAI normalized I/O", () => {+ it.each([+ openAiChatCompletionToolSequenceFixture,+ openAiChatMultimodalRichResponseFixture,+ openAiResponsesFunctionCallFixture,+ openAiResponsesBuiltInToolsAndMediaFixture,+ openAiResponsesReasoningWithParallelCallsFixture,+ ])("$name", ({ spanIO, expected }) => {+ expect(normalizeSpanIO(spanIO)).toEqual({+ ...expected,+ span: spanIO,+ });+ });+});diff --git a/packages/shared/src/utils/normalized-io/conventions/providers/otel-genai/fixtures.ts b/packages/shared/src/utils/normalized-io/conventions/providers/otel-genai/fixtures.tsnew file mode 100644index 0000000000..48cb920a77--- /dev/null+++ b/packages/shared/src/utils/normalized-io/conventions/providers/otel-genai/fixtures.ts@@ -0,0 +1,785 @@+import type { NormalizedIOFixture } from "../fixture-types";++export const microsoftAgentProductionShapeFixture = {+ name: "normalizes an anonymized Microsoft Agent Framework span",+ otel: {+ scopeSpan: {+ scope: {+ name: "agent_framework",+ version: "0.0.0",+ },+ spans: [+ {+ traceId: Buffer.from("00000000000000000000000000000001", "hex"),+ spanId: Buffer.from("0000000000000003", "hex"),+ parentSpanId: Buffer.from("0000000000000002", "hex"),+ name: "synthetic_name_002",+ kind: 1,+ attributes: [+ {+ key: "gen_ai.input.messages",+ value: {+ stringValue:+ '[{"role":"system","parts":[{"type":"text","content":"You are a helpful synthetic assistant 011."}]},{"role":"user","parts":[{"type":"text","content":"Synthetic user request 012."}]}]',+ },+ },+ {+ key: "gen_ai.system_instructions",+ value: {+ stringValue:+ '[{"type":"text","content":"synthetic-value-013"}]',+ },+ },+ {+ key: "gen_ai.request.choice.count",+ value: {+ intValue: "synthetic-value-014",+ },+ },+ {+ key: "gen_ai.operation.name",+ value: {+ stringValue: "invoke_agent",+ },+ },+ {+ key: "gen_ai.provider.name",+ value: {+ stringValue: "microsoft.agent_framework",+ },+ },+ {+ key: "gen_ai.agent.id",+ value: {+ stringValue: "synthetic-value-015",+ },+ },+ {+ key: "gen_ai.agent.name",+ value: {+ stringValue: "synthetic-value-016",+ },+ },+ {+ key: "gen_ai.request.model",+ value: {+ stringValue: "gpt-5.4",+ },+ },+ {+ key: "gen_ai.tool.definitions",+ value: {+ stringValue:+ '[{"type":"function","function":{"name":"synthetic_tool_001","description":"Synthetic description 017.","parameters":{"synthetic_field_001":{"synthetic_field_002":{"enum":["synthetic-value-018","synthetic-value-019","synthetic-value-020","synthetic-value-021","synthetic-value-022","synthetic-value-023"],"synthetic_field_003":"synthetic-value-024","type":"string"}},"properties":{"synthetic_field_004":{"anyOf":[{"type":"string"},{"type":"null"}],"synthetic_field_005":null,"description":"Synthetic description 025.","synthetic_field_003":"synthetic-value-026"},"synthetic_field_006":{"anyOf":[{"type":"string"},{"type":"null"}],"synthetic_field_005":null,"description":"Synthetic description 027.","synthetic_field_003":"synthetic-value-028"},"synthetic_field_007":{"anyOf":[{"enum":["synthetic-value-029","synthetic-value-030"],"type":"string"},{"type":"null"}],"synthetic_field_005":null,"description":"Synthetic description 031.","synthetic_field_003":"synthetic-value-032"},"synthetic_field_008":{"anyOf":[{"enum":["synthetic-value-033","synthetic-value-034"],"type":"string"},{"type":"null"}],"synthetic_field_005":null,"description":"Synthetic description 035.","synthetic_field_003":"synthetic-value-036"},"synthetic_field_009":{"anyOf":[{"items":{"synthetic_field_010":"synthetic-value-037"},"type":"synthetic-value-038"},{"type":"null"}],"synthetic_field_005":null,"description":"Synthetic description 039.","synthetic_field_003":"synthetic-value-040"},"synthetic_field_011":{"anyOf":[{"type":"string"},{"type":"null"}],"synthetic_field_005":null,"description":"Synthetic description 041.","synthetic_field_003":"synthetic-value-042"},"synthetic_field_012":{"anyOf":[{"type":"string"},{"type":"null"}],"synthetic_field_005":null,"description":"Synthetic description 043.","synthetic_field_003":"synthetic-value-044"},"synthetic_field_013":{"synthetic_field_005":false,"description":"Synthetic description 045.","synthetic_field_003":"synthetic-value-046","type":"boolean"}},"synthetic_field_003":"synthetic-value-047","type":"object"}}},{"type":"function","function":{"name":"synthetic_tool_002","description":"Synthetic description 048.","parameters":{"synthetic_field_001":{"synthetic_field_014":{"description":"Synthetic description 049.","properties":{"synthetic_field_015":{"format":"synthetic-value-050","synthetic_field_003":"synthetic-value-051","type":"string"},"synthetic_field_016":{"synthetic_field_003":"synthetic-value-052","type":"string"}},"required":["synthetic_field_015","synthetic_field_016"],"synthetic_field_003":"synthetic-value-053","type":"object"}},"properties":{"synthetic_field_017":{"description":"Synthetic description 054.","synthetic_field_003":"synthetic-value-055","type":"string"},"synthetic_field_018":{"anyOf":[{"items":{"type":"string"},"type":"synthetic-value-056"},{"type":"null"}],"synthetic_field_005":null,"description":"Synthetic description 057.","synthetic_field_003":"synthetic-value-058"},"synthetic_field_019":{"anyOf":[{"additionalProperties":{"type":"string"},"type":"object"},{"type":"null"}],"synthetic_field_005":null,"description":"Synthetic description 059.","synthetic_field_003":"synthetic-value-060"},"synthetic_field_020":{"anyOf":[{"items":{"synthetic_field_010":"synthetic-value-061"},"type":"synthetic-value-062"},{"type":"null"}],"synthetic_field_005":null,"description":"Synthetic description 063.","synthetic_field_003":"synthetic-value-064"},"synthetic_field_021":{"anyOf":[{"enum":["synthetic-value-065","synthetic-value-066"],"type":"string"},{"type":"null"}],"synthetic_field_005":null,"description":"Synthetic description 067.","synthetic_field_003":"synthetic-value-068"},"synthetic_field_022":{"anyOf":[{"type":"string"},{"type":"null"}],"synthetic_field_005":null,"description":"Synthetic description 069.","synthetic_field_003":"synthetic-value-070"}},"required":["synthetic_field_017"],"synthetic_field_003":"synthetic-value-071","type":"object"}}},{"type":"function","function":{"name":"synthetic_tool_003","description":"Synthetic description 072.","parameters":{"properties":{"synthetic_field_017":{"description":"Synthetic description 073.","synthetic_field_003":"synthetic-value-074","type":"string"}},"required":["synthetic_field_017"],"synthetic_field_003":"synthetic-value-075","type":"object"}}},{"type":"function","function":{"name":"synthetic_tool_004","description":"Synthetic description 076.","parameters":{"properties":{"synthetic_field_011":{"description":"Synthetic description 077.","synthetic_field_003":"synthetic-value-078","type":"string"}},"required":["synthetic_field_011"],"synthetic_field_003":"synthetic-value-079","type":"object"}}},{"type":"function","function":{"name":"synthetic_tool_005","description":"Synthetic description 080.","parameters":{"properties":{"synthetic_field_023":{"description":"Synthetic description 081.","synthetic_field_003":"synthetic-value-082","type":"string"},"synthetic_field_024":{"description":"Synthetic description 083.","synthetic_field_003":"synthetic-value-084","type":"string"}},"required":["synthetic_field_023","synthetic_field_024"],"synthetic_field_003":"synthetic-value-085","type":"object"}}}]',+ },+ },+ {+ key: "langfuse.observation.type",+ value: {+ stringValue: "synthetic-value-086",+ },+ },+ {+ key: "gen_ai.output.messages",+ value: {+ stringValue:+ '[{"role":"assistant","parts":[{"type":"text","content":"Synthetic assistant response 087."}]}]',+ },+ },+ ],+ status: {},+ },+ ],+ },+ resourceAttributes: {+ "telemetry.sdk.language": "python",+ "telemetry.sdk.name": "opentelemetry",+ "telemetry.sdk.version": "1.41.1",+ "service.name": "synthetic-service",+ },+ },+ spanIO: {+ input: {+ messages: [+ {+ role: "system",+ parts: [+ {+ type: "text",+ content: "You are a helpful synthetic assistant 011.",+ },+ ],+ },+ {+ role: "user",+ parts: [+ {+ type: "text",+ content: "Synthetic user request 012.",+ },+ ],+ },+ ],+ tools: [+ {+ type: "function",+ function: {+ name: "synthetic_tool_001",+ description: "Synthetic description 017.",+ parameters: {+ synthetic_field_001: {+ synthetic_field_002: {+ enum: [+ "synthetic-value-018",+ "synthetic-value-019",+ "synthetic-value-020",+ "synthetic-value-021",+ "synthetic-value-022",+ "synthetic-value-023",+ ],+ synthetic_field_003: "synthetic-value-024",+ type: "string",+ },+ },+ properties: {+ synthetic_field_004: {+ anyOf: [+ {+ type: "string",+ },+ {+ type: "null",+ },+ ],+ synthetic_field_005: null,+ description: "Synthetic description 025.",+ synthetic_field_003: "synthetic-value-026",+ },+ synthetic_field_006: {+ anyOf: [+ {+ type: "string",+ },+ {+ type: "null",+ },+ ],+ synthetic_field_005: null,+ description: "Synthetic description 027.",+ synthetic_field_003: "synthetic-value-028",+ },+ synthetic_field_007: {+ anyOf: [+ {+ enum: ["synthetic-value-029", "synthetic-value-030"],+ type: "string",+ },+ {+ type: "null",+ },+ ],+ synthetic_field_005: null,+ description: "Synthetic description 031.",+ synthetic_field_003: "synthetic-value-032",+ },+ synthetic_field_008: {+ anyOf: [+ {+ enum: ["synthetic-value-033", "synthetic-value-034"],+ type: "string",+ },+ {+ type: "null",+ },+ ],+ synthetic_field_005: null,+ description: "Synthetic description 035.",+ synthetic_field_003: "synthetic-value-036",+ },+ synthetic_field_009: {+ anyOf: [+ {+ items: {+ synthetic_field_010: "synthetic-value-037",+ },+ type: "synthetic-value-038",+ },+ {+ type: "null",+ },+ ],+ synthetic_field_005: null,+ description: "Synthetic description 039.",+ synthetic_field_003: "synthetic-value-040",+ },+ synthetic_field_011: {+ anyOf: [+ {+ type: "string",+ },+ {+ type: "null",+ },+ ],+ synthetic_field_005: null,+ description: "Synthetic description 041.",+ synthetic_field_003: "synthetic-value-042",+ },+ synthetic_field_012: {+ anyOf: [+ {+ type: "string",+ },+ {+ type: "null",+ },+ ],+ synthetic_field_005: null,+ description: "Synthetic description 043.",+ synthetic_field_003: "synthetic-value-044",+ },+ synthetic_field_013: {+ synthetic_field_005: false,+ description: "Synthetic description 045.",+ synthetic_field_003: "synthetic-value-046",+ type: "boolean",+ },+ },+ synthetic_field_003: "synthetic-value-047",+ type: "object",+ },+ },+ },+ {+ type: "function",+ function: {+ name: "synthetic_tool_002",+ description: "Synthetic description 048.",+ parameters: {+ synthetic_field_001: {+ synthetic_field_014: {+ description: "Synthetic description 049.",+ properties: {+ synthetic_field_015: {+ format: "synthetic-value-050",+ synthetic_field_003: "synthetic-value-051",+ type: "string",+ },+ synthetic_field_016: {+ synthetic_field_003: "synthetic-value-052",+ type: "string",+ },+ },+ required: ["synthetic_field_015", "synthetic_field_016"],+ synthetic_field_003: "synthetic-value-053",+ type: "object",+ },+ },+ properties: {+ synthetic_field_017: {+ description: "Synthetic description 054.",+ synthetic_field_003: "synthetic-value-055",+ type: "string",+ },+ synthetic_field_018: {+ anyOf: [+ {+ items: {+ type: "string",+ },+ type: "synthetic-value-056",+ },+ {+ type: "null",+ },+ ],+ synthetic_field_005: null,+ description: "Synthetic description 057.",+ synthetic_field_003: "synthetic-value-058",+ },+ synthetic_field_019: {+ anyOf: [+ {+ additionalProperties: {+ type: "string",+ },+ type: "object",+ },+ {+ type: "null",+ },+ ],+ synthetic_field_005: null,+ description: "Synthetic description 059.",+ synthetic_field_003: "synthetic-value-060",+ },+ synthetic_field_020: {+ anyOf: [+ {+ items: {+ synthetic_field_010: "synthetic-value-061",+ },+ type: "synthetic-value-062",+ },+ {+ type: "null",+ },+ ],+ synthetic_field_005: null,+ description: "Synthetic description 063.",+ synthetic_field_003: "synthetic-value-064",+ },+ synthetic_field_021: {+ anyOf: [+ {+ enum: ["synthetic-value-065", "synthetic-value-066"],+ type: "string",+ },+ {+ type: "null",+ },+ ],+ synthetic_field_005: null,+ description: "Synthetic description 067.",+ synthetic_field_003: "synthetic-value-068",+ },+ synthetic_field_022: {+ anyOf: [+ {+ type: "string",+ },+ {+ type: "null",+ },+ ],+ synthetic_field_005: null,+ description: "Synthetic description 069.",+ synthetic_field_003: "synthetic-value-070",+ },+ },+ required: ["synthetic_field_017"],+ synthetic_field_003: "synthetic-value-071",+ type: "object",+ },+ },+ },+ {+ type: "function",+ function: {+ name: "synthetic_tool_003",+ description: "Synthetic description 072.",+ parameters: {+ properties: {+ synthetic_field_017: {+ description: "Synthetic description 073.",+ synthetic_field_003: "synthetic-value-074",+ type: "string",+ },+ },+ required: ["synthetic_field_017"],+ synthetic_field_003: "synthetic-value-075",+ type: "object",+ },+ },+ },+ {+ type: "function",+ function: {+ name: "synthetic_tool_004",+ description: "Synthetic description 076.",+ parameters: {+ properties: {+ synthetic_field_011: {+ description: "Synthetic description 077.",+ synthetic_field_003: "synthetic-value-078",+ type: "string",+ },+ },+ required: ["synthetic_field_011"],+ synthetic_field_003: "synthetic-value-079",+ type: "object",+ },+ },+ },+ {+ type: "function",+ function: {+ name: "synthetic_tool_005",+ description: "Synthetic description 080.",+ parameters: {+ properties: {+ synthetic_field_023: {+ description: "Synthetic description 081.",+ synthetic_field_003: "synthetic-value-082",+ type: "string",+ },+ synthetic_field_024: {+ description: "Synthetic description 083.",+ synthetic_field_003: "synthetic-value-084",+ type: "string",+ },+ },+ required: ["synthetic_field_023", "synthetic_field_024"],+ synthetic_field_003: "synthetic-value-085",+ type: "object",+ },+ },+ },+ ],+ },+ output:+ '[{"role":"assistant","parts":[{"type":"text","content":"Synthetic assistant response 087."}]}]',+ metadata: {+ attributes: {+ "gen_ai.request.choice.count": '{"intValue":"synthetic-value-014"}',+ "gen_ai.operation.name": "invoke_agent",+ "gen_ai.provider.name": "microsoft.agent_framework",+ "gen_ai.agent.id": "synthetic-value-015",+ "gen_ai.agent.name": "synthetic-value-016",+ "gen_ai.request.model": "gpt-5.4",+ "langfuse.observation.type": "synthetic-value-086",+ },+ resourceAttributes: {+ "telemetry.sdk.language": "python",+ "telemetry.sdk.name": "opentelemetry",+ "telemetry.sdk.version": "1.41.1",+ "service.name": "synthetic-service",+ },+ scope: {+ name: "agent_framework",+ version: "0.0.0",+ attributes: {},+ },+ },+ },+ expected: {+ messages: [+ {+ role: "system",+ parts: [+ {+ type: "text",+ text: "You are a helpful synthetic assistant 011.",+ },+ ],+ source: "input",+ },+ {+ role: "user",+ parts: [+ {+ type: "text",+ text: "Synthetic user request 012.",+ },+ ],+ source: "input",+ },+ {+ role: "assistant",+ parts: [+ {+ type: "text",+ text: "Synthetic assistant response 087.",+ },+ ],+ source: "output",+ },+ ],+ toolDefinitions: [+ {+ name: "synthetic_tool_001",+ description: "Synthetic description 017.",+ inputSchema: {+ synthetic_field_001: {+ synthetic_field_002: {+ enum: [+ "synthetic-value-018",+ "synthetic-value-019",+ "synthetic-value-020",+ "synthetic-value-021",+ "synthetic-value-022",+ "synthetic-value-023",+ ],+ synthetic_field_003: "synthetic-value-024",+ type: "string",+ },+ },+ properties: {+ synthetic_field_004: {+ anyOf: [+ {+ type: "string",+ },+ {+ type: "null",+ },+ ],+ synthetic_field_005: null,+ description: "Synthetic description 025.",+ synthetic_field_003: "synthetic-value-026",+ },+ synthetic_field_006: {+ anyOf: [+ {+ type: "string",+ },+ {+ type: "null",+ },+ ],+ synthetic_field_005: null,+ description: "Synthetic description 027.",+ synthetic_field_003: "synthetic-value-028",+ },+ synthetic_field_007: {+ anyOf: [+ {+ enum: ["synthetic-value-029", "synthetic-value-030"],+ type: "string",+ },+ {+ type: "null",+ },+ ],+ synthetic_field_005: null,+ description: "Synthetic description 031.",+ synthetic_field_003: "synthetic-value-032",+ },+ synthetic_field_008: {+ anyOf: [+ {+ enum: ["synthetic-value-033", "synthetic-value-034"],+ type: "string",+ },+ {+ type: "null",+ },+ ],+ synthetic_field_005: null,+ description: "Synthetic description 035.",+ synthetic_field_003: "synthetic-value-036",+ },+ synthetic_field_009: {+ anyOf: [+ {+ items: {+ synthetic_field_010: "synthetic-value-037",+ },+ type: "synthetic-value-038",+ },+ {+ type: "null",+ },+ ],+ synthetic_field_005: null,+ description: "Synthetic description 039.",+ synthetic_field_003: "synthetic-value-040",+ },+ synthetic_field_011: {+ anyOf: [+ {+ type: "string",+ },+ {+ type: "null",+ },+ ],+ synthetic_field_005: null,+ description: "Synthetic description 041.",+ synthetic_field_003: "synthetic-value-042",+ },+ synthetic_field_012: {+ anyOf: [+ {+ type: "string",+ },+ {+ type: "null",+ },+ ],+ synthetic_field_005: null,+ description: "Synthetic description 043.",+ synthetic_field_003: "synthetic-value-044",+ },+ synthetic_field_013: {+ synthetic_field_005: false,+ description: "Synthetic description 045.",+ synthetic_field_003: "synthetic-value-046",+ type: "boolean",+ },+ },+ synthetic_field_003: "synthetic-value-047",+ type: "object",+ },+ type: "function",+ },+ {+ name: "synthetic_tool_002",+ description: "Synthetic description 048.",+ inputSchema: {+ synthetic_field_001: {+ synthetic_field_014: {+ description: "Synthetic description 049.",+ properties: {+ synthetic_field_015: {+ format: "synthetic-value-050",+ synthetic_field_003: "synthetic-value-051",+ type: "string",+ },+ synthetic_field_016: {+ synthetic_field_003: "synthetic-value-052",+ type: "string",+ },+ },+ required: ["synthetic_field_015", "synthetic_field_016"],+ synthetic_field_003: "synthetic-value-053",+ type: "object",+ },+ },+ properties: {+ synthetic_field_017: {+ description: "Synthetic description 054.",+ synthetic_field_003: "synthetic-value-055",+ type: "string",+ },+ synthetic_field_018: {+ anyOf: [+ {+ items: {+ type: "string",+ },+ type: "synthetic-value-056",+ },+ {+ type: "null",+ },+ ],+ synthetic_field_005: null,+ description: "Synthetic description 057.",+ synthetic_field_003: "synthetic-value-058",+ },+ synthetic_field_019: {+ anyOf: [+ {+ additionalProperties: {+ type: "string",+ },+ type: "object",+ },+ {+ type: "null",+ },+ ],+ synthetic_field_005: null,+ description: "Synthetic description 059.",+ synthetic_field_003: "synthetic-value-060",+ },+ synthetic_field_020: {+ anyOf: [+ {+ items: {+ synthetic_field_010: "synthetic-value-061",+ },+ type: "synthetic-value-062",+ },+ {+ type: "null",+ },+ ],+ synthetic_field_005: null,+ description: "Synthetic description 063.",+ synthetic_field_003: "synthetic-value-064",+ },+ synthetic_field_021: {+ anyOf: [+ {+ enum: ["synthetic-value-065", "synthetic-value-066"],+ type: "string",+ },+ {+ type: "null",+ },+ ],+ synthetic_field_005: null,+ description: "Synthetic description 067.",+ synthetic_field_003: "synthetic-value-068",+ },+ synthetic_field_022: {+ anyOf: [+ {+ type: "string",+ },+ {+ type: "null",+ },+ ],+ synthetic_field_005: null,+ description: "Synthetic description 069.",+ synthetic_field_003: "synthetic-value-070",+ },+ },+ required: ["synthetic_field_017"],+ synthetic_field_003: "synthetic-value-071",+ type: "object",+ },+ type: "function",+ },+ {+ name: "synthetic_tool_003",+ description: "Synthetic description 072.",+ inputSchema: {+ properties: {+ synthetic_field_017: {+ description: "Synthetic description 073.",+ synthetic_field_003: "synthetic-value-074",+ type: "string",+ },+ },+ required: ["synthetic_field_017"],+ synthetic_field_003: "synthetic-value-075",+ type: "object",+ },+ type: "function",+ },+ {+ name: "synthetic_tool_004",+ description: "Synthetic description 076.",+ inputSchema: {+ properties: {+ synthetic_field_011: {+ description: "Synthetic description 077.",+ synthetic_field_003: "synthetic-value-078",+ type: "string",+ },+ },+ required: ["synthetic_field_011"],+ synthetic_field_003: "synthetic-value-079",+ type: "object",+ },+ type: "function",+ },+ {+ name: "synthetic_tool_005",+ description: "Synthetic description 080.",+ inputSchema: {+ properties: {+ synthetic_field_023: {+ description: "Synthetic description 081.",+ synthetic_field_003: "synthetic-value-082",+ type: "string",+ },+ synthetic_field_024: {+ description: "Synthetic description 083.",+ synthetic_field_003: "synthetic-value-084",+ type: "string",+ },+ },+ required: ["synthetic_field_023", "synthetic_field_024"],+ synthetic_field_003: "synthetic-value-085",+ type: "object",+ },+ type: "function",+ },+ ],+ },+} satisfies NormalizedIOFixture;diff --git a/packages/shared/src/utils/normalized-io/conventions/providers/otel-genai/index.ts b/packages/shared/src/utils/normalized-io/conventions/providers/otel-genai/index.tsnew file mode 100644index 0000000000..5fc374069c--- /dev/null+++ b/packages/shared/src/utils/normalized-io/conventions/providers/otel-genai/index.ts@@ -0,0 +1,148 @@+import { claimed, dropped, unmatched } from "../..";+import { asRecord, compact, optionalString } from "../../../core/utils/json";+import { toolCallPart } from "../../../core/normalize/message-parts/tool-calls";+import { toolResultPart } from "../../../core/normalize/message-parts/tool-results";+import type {+ FinishReason,+ NormalizedMessage,+ ToolResultPart,+} from "../../../types";+import type {+ ConventionResult,+ IOConvention,+ MessageEnvelopeContext,+ PartHandler,+ ToolDefinitionCarrier,+ ToolDefinitionSource,+} from "../../io-convention";++/**+ * OTel GenAI event-stream convention (the semconv vocabulary also emitted by+ * Traceloop/OpenLLMetry and koog-style conversions): this module owns+ * choice-event envelopes, the snake_case `tool_call`/`tool_call_response`+ * types no SDK's own vocabulary claims, and the `gen_ai.tool.definitions`/+ * `llm.tools.N` attribute carriers.+ */+function normalizeOtelGenaiToolResult(+ value: Record<string, unknown>,+): ConventionResult<ToolResultPart> {+ return claimed(+ toolResultPart({+ toolCallId:+ value.toolCallId /* ai sdk */ ??+ value.tool_use_id /* anthropic */ ??+ value.call_id /* openai responses */ ??+ value.id,+ toolName: value.toolName /* ai sdk */ ?? value.tool_name /* otel */,+ output:+ value.output /* ai sdk, openai responses */ ??+ value.response /* gemini */ ??+ value.result /* loose/traceloop */ ??+ value.content /* anthropic, openai chat message */,+ isError: [+ value.is_error /* anthropic */,+ value.isError /* ai sdk */,+ ].find((candidate) => typeof candidate === "boolean") as+ | boolean+ | undefined,+ }),+ );+}++const OTEL_GENAI_FINISH_REASON_TYPE_BY_RAW: Record<+ string,+ FinishReason["type"]+> = {+ tool_call: "tool-calls",+};++const OTEL_GENAI_PART_HANDLERS = {+ tool_call: (value: Record<string, unknown>) =>+ claimed(+ toolCallPart({+ toolCallId: value.toolCallId /* ai sdk */ ?? value.call_id ?? value.id,+ toolName: value.toolName /* ai sdk */ ?? value.name,+ input:+ value.input ??+ value.arguments /* otel */ ??+ value.args /* langchain */,+ toolType: optionalString(value.type),+ }),+ ),+ tool_call_response: (value: Record<string, unknown>) =>+ normalizeOtelGenaiToolResult(value),+} satisfies Readonly<Record<string, PartHandler>>;++/** GenAI choice events: `{index?, message, finish_reason?}`. */+function unwrapOtelGenaiChoiceEvent(+ value: Record<string, unknown>,+ fallbackRole: "user" | "assistant",+ ctx: MessageEnvelopeContext,+): ConventionResult<NormalizedMessage> {+ if (ctx.isMessageLike(value)) return unmatched;+ const nestedMessage = asRecord(value.message);+ if (!nestedMessage) return unmatched;++ const message = ctx.normalizeMessage(nestedMessage, fallbackRole);+ if (!message) return dropped;++ const finishReason = ctx.normalizeFinishReason(value) ?? message.finishReason;+ return claimed(compact({ ...message, finishReason }));+}++/**+ * Tool definitions among span attributes: the semconv+ * `gen_ai.tool.definitions` list, and Traceloop/OpenLLMetry's indexed+ * `llm.tools.N.tool.json_schema` keys (returned in index order).+ */+function otelGenaiToolDefinitionSources(+ carrier: ToolDefinitionCarrier,+): ToolDefinitionSource[] {+ const attributes = carrier.metadataAttributes;+ if (!attributes) return [];++ const sources: ToolDefinitionSource[] = [];++ if (attributes["gen_ai.tool.definitions"] !== undefined) {+ sources.push({+ sourceKey: "gen_ai.tool.definitions",+ value: attributes["gen_ai.tool.definitions"],+ options: { allowToolMap: true },+ });+ }++ const indexedToolKeys = Object.keys(attributes)+ .map((key) => ({+ key,+ index: /^llm\.tools\.(\d+)\.tool\.json_schema$/.exec(key)?.[1],+ }))+ .filter(+ (entry): entry is { key: string; index: string } =>+ entry.index !== undefined,+ )+ .sort((left, right) => Number(left.index) - Number(right.index));+ for (const { key } of indexedToolKeys) {+ sources.push({ sourceKey: key, value: attributes[key] });+ }++ return sources;+}++export const otelGenaiProvider = {+ name: "otel-genai",+ finishReasonTypeByRaw: OTEL_GENAI_FINISH_REASON_TYPE_BY_RAW,+ typedParts: OTEL_GENAI_PART_HANDLERS,+ tryUnwrapMessage: unwrapOtelGenaiChoiceEvent,+ // koog/Traceloop role-"tool" turns that are actually tool *definitions*+ // (an OpenAI function-tool wrapper with no tool_call_id) β not content.+ isToolDefinitionMessage: (message: Record<string, unknown>) => {+ const content = asRecord(message.content);+ return Boolean(+ message.role === "tool" &&+ content?.type === "function" &&+ content.function &&+ !message.tool_call_id,+ );+ },+ collectToolDefinitionSources: otelGenaiToolDefinitionSources,+} satisfies IOConvention;diff --git a/packages/shared/src/utils/normalized-io/conventions/providers/otel-genai/otel-genai.test.ts b/packages/shared/src/utils/normalized-io/conventions/providers/otel-genai/otel-genai.test.tsnew file mode 100644index 0000000000..776b326e0a--- /dev/null+++ b/packages/shared/src/utils/normalized-io/conventions/providers/otel-genai/otel-genai.test.ts@@ -0,0 +1,16 @@+import { describe, expect, it } from "vitest";++import { normalizeSpanIO } from "../../../parser";+import { microsoftAgentProductionShapeFixture } from "./fixtures";++describe("OTel GenAI normalized I/O", () => {+ it.each([microsoftAgentProductionShapeFixture])(+ "$name",+ ({ spanIO, expected }) => {+ expect(normalizeSpanIO(spanIO)).toEqual({+ ...expected,+ span: spanIO,+ });+ },+ );+});diff --git a/packages/shared/src/utils/normalized-io/conventions/providers/pydantic-ai/fixtures.ts b/packages/shared/src/utils/normalized-io/conventions/providers/pydantic-ai/fixtures.tsnew file mode 100644index 0000000000..75996a8aaa--- /dev/null+++ b/packages/shared/src/utils/normalized-io/conventions/providers/pydantic-ai/fixtures.ts@@ -0,0 +1,2455 @@+import type { NormalizedIOFixture } from "../fixture-types";++export const pydanticAiProductionShapeFixture = {+ name: "normalizes an anonymized Pydantic AI span",+ otel: {+ scopeSpan: {+ scope: {+ name: "pydantic-ai",+ version: "2.29.0",+ },+ spans: [+ {+ traceId: Buffer.from("00000000000000000000000000000001", "hex"),+ spanId: Buffer.from("0000000000000001", "hex"),+ parentSpanId: Buffer.from("0000000000000002", "hex"),+ name: "synthetic_name_001",+ kind: 3,+ attributes: [+ {+ key: "gen_ai.operation.name",+ value: {+ stringValue: "chat",+ },+ },+ {+ key: "gen_ai.provider.name",+ value: {+ stringValue: "openai",+ },+ },+ {+ key: "gen_ai.system",+ value: {+ stringValue: "synthetic-value-015",+ },+ },+ {+ key: "synthetic.metadata.006",+ value: {+ stringValue: "synthetic-value-016",+ },+ },+ {+ key: "gen_ai.request.model",+ value: {+ stringValue: "gpt-5.4-mini-2026-03-17",+ },+ },+ {+ key: "gen_ai.agent.name",+ value: {+ stringValue: "synthetic-value-017",+ },+ },+ {+ key: "gen_ai.agent.call.id",+ value: {+ stringValue: "synthetic-value-018",+ },+ },+ {+ key: "gen_ai.conversation.id",+ value: {+ stringValue: "synthetic-value-019",+ },+ },+ {+ key: "synthetic.metadata.007",+ value: {+ stringValue:+ '{"synthetic_field_006":[{"name":"synthetic_name_002","parameters_json_schema":{"additionalProperties":false,"properties":{"url":{"type":"string"}},"required":["synthetic_field_007"],"type":"object"},"description":"Synthetic description 020.","synthetic_field_008":null,"synthetic_field_009":true,"synthetic_field_010":false,"kind":"function","synthetic_field_011":null,"synthetic_field_012":null,"synthetic_field_013":false,"synthetic_field_014":null,"synthetic_field_015":null,"synthetic_field_016":null,"synthetic_field_017":null,"synthetic_field_018":null,"synthetic_field_019":"synthetic_id_001","synthetic_field_020":null},{"name":"synthetic_name_003","parameters_json_schema":{"additionalProperties":false,"properties":{},"type":"object"},"description":"Synthetic description 021.","synthetic_field_008":null,"synthetic_field_009":false,"synthetic_field_010":false,"kind":"function","synthetic_field_011":null,"synthetic_field_012":null,"synthetic_field_013":false,"synthetic_field_014":null,"synthetic_field_015":null,"synthetic_field_016":null,"synthetic_field_017":null,"synthetic_field_018":null,"synthetic_field_019":"synthetic_id_001","synthetic_field_020":null},{"name":"synthetic_name_004","parameters_json_schema":{"additionalProperties":false,"synthetic_field_021":[{"synthetic_field_022":"synthetic_id_002"}],"properties":{"synthetic_field_022":{"description":"Synthetic description 022.","format":"synthetic-value-023","type":"string"}},"required":["synthetic_field_022"],"type":"object"},"description":"Synthetic description 024.","synthetic_field_008":null,"synthetic_field_009":true,"synthetic_field_010":false,"kind":"function","synthetic_field_011":null,"synthetic_field_012":null,"synthetic_field_013":false,"synthetic_field_014":null,"synthetic_field_015":null,"synthetic_field_016":null,"synthetic_field_017":null,"synthetic_field_018":null,"synthetic_field_019":"synthetic_id_001","synthetic_field_020":null},{"name":"synthetic_name_005","parameters_json_schema":{"additionalProperties":false,"synthetic_field_021":[{"synthetic_field_023":"synthetic-value-025","synthetic_field_024":"synthetic-value-026","synthetic_field_025":"synthetic-value-027","synthetic_field_026":"synthetic-value-028","synthetic_field_027":"synthetic-value-029","synthetic_field_028":"synthetic-value-030","synthetic_field_029":"synthetic-value-031","synthetic_field_030":"synthetic-value-032","synthetic_field_031":85000,"synthetic_field_032":65000,"synthetic_field_033":"synthetic-value-033","synthetic_field_034":"synthetic-value-034"}],"properties":{"synthetic_field_034":{"description":"Synthetic description 035.","synthetic_field_021":["synthetic-value-036"],"synthetic_field_035":255,"synthetic_field_036":1,"type":"string"},"synthetic_field_029":{"synthetic_field_037":null,"description":"Synthetic description 037.","anyOf":[{"type":"string"},{"type":"null"}]},"synthetic_field_026":{"synthetic_field_037":null,"description":"Synthetic description 038.","anyOf":[{"type":"string"},{"type":"null"}]},"synthetic_field_027":{"synthetic_field_037":null,"description":"Synthetic description 039.","anyOf":[{"type":"string"},{"type":"null"}]},"synthetic_field_023":{"synthetic_field_037":null,"description":"Synthetic description 040.","anyOf":[{"type":"string"},{"type":"null"}]},"synthetic_field_024":{"synthetic_field_037":null,"description":"Synthetic description 041.","anyOf":[{"type":"string"},{"type":"null"}]},"synthetic_field_025":{"synthetic_field_037":null,"description":"Synthetic description 042.","synthetic_field_021":["synthetic-value-043"],"anyOf":[{"synthetic_field_038":"synthetic-value-044"},{"type":"null"}]},"synthetic_field_028":{"synthetic_field_037":null,"description":"Synthetic description 045.","synthetic_field_021":["synthetic-value-046"],"anyOf":[{"synthetic_field_038":"synthetic-value-047"},{"type":"null"}]},"synthetic_field_033":{"synthetic_field_037":null,"description":"Synthetic description 048.","anyOf":[{"type":"string"},{"type":"null"}]},"synthetic_field_039":{"synthetic_field_037":null,"description":"Synthetic description 049.","anyOf":[{"items":{"synthetic_field_038":"synthetic-value-050"},"type":"synthetic-value-051"},{"type":"null"}]},"synthetic_field_030":{"synthetic_field_037":null,"description":"Synthetic description 052.","synthetic_field_021":["synthetic-value-053"],"anyOf":[{"synthetic_field_038":"synthetic-value-054"},{"type":"null"}]},"synthetic_field_032":{"synthetic_field_037":null,"description":"Synthetic description 055.","anyOf":[{"type":"integer"},{"type":"null"}]},"synthetic_field_031":{"synthetic_field_037":null,"description":"Synthetic description 056.","anyOf":[{"type":"integer"},{"type":"null"}]},"synthetic_field_040":{"synthetic_field_037":null,"description":"Synthetic description 057.","anyOf":[{"type":"integer"},{"type":"null"}]},"synthetic_field_041":{"synthetic_field_037":null,"description":"Synthetic description 058.","anyOf":[{"type":"integer"},{"type":"null"}]}},"required":["synthetic_field_034"],"type":"object","synthetic_field_042":{"synthetic_field_043":{"enum":["synthetic-value-059","synthetic-value-060","synthetic-value-061","synthetic-value-062","synthetic-value-063","synthetic-value-064","synthetic-value-065","synthetic-value-066","synthetic-value-067","synthetic-value-068"],"type":"string"},"synthetic_field_044":{"enum":["synthetic-value-069","synthetic-value-070","synthetic-value-071"],"type":"string"},"synthetic_field_045":{"enum":["synthetic-value-072","synthetic-value-073","synthetic-value-074"],"type":"string"},"synthetic_field_046":{"properties":{"synthetic_field_047":{"description":"Synthetic description 075.","type":"string"},"synthetic_field_048":{"synthetic_field_037":null,"description":"Synthetic description 076.","synthetic_field_021":["synthetic-value-077"],"anyOf":[{"type":"string"},{"type":"null"}]},"synthetic_field_049":{"synthetic_field_037":null,"description":"Synthetic description 078.","synthetic_field_021":["synthetic-value-079"],"anyOf":[{"type":"string"},{"type":"null"}]},"synthetic_field_050":{"synthetic_field_037":null,"description":"Synthetic description 080.","anyOf":[{"type":"string"},{"type":"null"}]},"synthetic_field_051":{"synthetic_field_037":null,"description":"Synthetic description 081.","anyOf":[{"type":"string"},{"type":"null"}]},"synthetic_field_052":{"synthetic_field_037":null,"description":"Synthetic description 082.","anyOf":[{"type":"string"},{"type":"null"}]},"synthetic_field_053":{"synthetic_field_037":null,"description":"Synthetic description 083.","anyOf":[{"type":"string"},{"type":"null"}]},"synthetic_field_054":{"description":"Synthetic description 084.","synthetic_field_021":["synthetic-value-085"],"type":"string"},"synthetic_field_055":{"synthetic_field_037":null,"description":"Synthetic description 086.","anyOf":[{"type":"string"},{"type":"null"}]},"synthetic_field_056":{"description":"Synthetic description 087.","type":"string"},"synthetic_field_057":{"synthetic_field_037":null,"description":"Synthetic description 088.","anyOf":[{"type":"string"},{"type":"null"}]},"synthetic_field_058":{"synthetic_field_037":null,"description":"Synthetic description 089.","anyOf":[{"type":"string"},{"type":"null"}]},"synthetic_field_059":{"synthetic_field_037":null,"description":"Synthetic description 090.","anyOf":[{"type":"string"},{"type":"null"}]}},"required":["synthetic_field_047","synthetic_field_054","synthetic_field_056"],"type":"object","additionalProperties":false}}},"description":"Synthetic description 091.","synthetic_field_008":null,"synthetic_field_009":false,"synthetic_field_010":false,"kind":"function","synthetic_field_011":null,"synthetic_field_012":null,"synthetic_field_013":false,"synthetic_field_014":null,"synthetic_field_015":null,"synthetic_field_016":null,"synthetic_field_017":null,"synthetic_field_018":null,"synthetic_field_019":"synthetic_id_001","synthetic_field_020":null},{"name":"synthetic_name_006","parameters_json_schema":{"additionalProperties":false,"synthetic_field_021":[{"synthetic_field_022":"synthetic_id_002","synthetic_field_028":"synthetic-value-092","synthetic_field_031":90000,"synthetic_field_032":70000,"synthetic_field_033":"synthetic-value-093"}],"properties":{"synthetic_field_022":{"description":"Synthetic description 094.","format":"synthetic-value-095","type":"string"},"synthetic_field_034":{"synthetic_field_037":null,"description":"Synthetic description 096.","synthetic_field_021":["synthetic-value-097"],"anyOf":[{"synthetic_field_035":255,"synthetic_field_036":1,"type":"string"},{"type":"null"}]},"synthetic_field_029":{"synthetic_field_037":null,"description":"Synthetic description 098.","anyOf":[{"type":"string"},{"type":"null"}]},"synthetic_field_026":{"synthetic_field_037":null,"description":"Synthetic description 099.","anyOf":[{"type":"string"},{"type":"null"}]},"synthetic_field_027":{"synthetic_field_037":null,"description":"Synthetic description 100.","anyOf":[{"type":"string"},{"type":"null"}]},"synthetic_field_023":{"synthetic_field_037":null,"description":"Synthetic description 101.","anyOf":[{"type":"string"},{"type":"null"}]},"synthetic_field_024":{"synthetic_field_037":null,"description":"Synthetic description 102.","anyOf":[{"type":"string"},{"type":"null"}]},"synthetic_field_025":{"synthetic_field_037":null,"description":"Synthetic description 103.","synthetic_field_021":["synthetic-value-104"],"anyOf":[{"synthetic_field_038":"synthetic-value-105"},{"type":"null"}]},"synthetic_field_028":{"synthetic_field_037":null,"description":"Synthetic description 106.","synthetic_field_021":["synthetic-value-107"],"anyOf":[{"synthetic_field_038":"synthetic-value-108"},{"type":"null"}]},"synthetic_field_033":{"synthetic_field_037":null,"description":"Synthetic description 109.","anyOf":[{"type":"string"},{"type":"null"}]},"synthetic_field_039":{"synthetic_field_037":null,"description":"Synthetic description 110.","anyOf":[{"items":{"synthetic_field_038":"synthetic-value-111"},"type":"synthetic-value-112"},{"type":"null"}]},"synthetic_field_030":{"synthetic_field_037":null,"description":"Synthetic description 113.","synthetic_field_021":["synthetic-value-114"],"anyOf":[{"synthetic_field_038":"synthetic-value-115"},{"type":"null"}]},"synthetic_field_032":{"synthetic_field_037":null,"description":"Synthetic description 116.","anyOf":[{"type":"integer"},{"type":"null"}]},"synthetic_field_031":{"synthetic_field_037":null,"description":"Synthetic description 117.","anyOf":[{"type":"integer"},{"type":"null"}]},"synthetic_field_040":{"synthetic_field_037":null,"description":"Synthetic description 118.","anyOf":[{"type":"integer"},{"type":"null"}]},"synthetic_field_041":{"synthetic_field_037":null,"description":"Synthetic description 119.","anyOf":[{"type":"integer"},{"type":"null"}]}},"required":["synthetic_field_022"],"type":"object","synthetic_field_042":{"synthetic_field_043":{"enum":["synthetic-value-120","synthetic-value-121","synthetic-value-122","synthetic-value-123","synthetic-value-124","synthetic-value-125","synthetic-value-126","synthetic-value-127","synthetic-value-128","synthetic-value-129"],"type":"string"},"synthetic_field_044":{"enum":["synthetic-value-130","synthetic-value-131","synthetic-value-132"],"type":"string"},"synthetic_field_045":{"enum":["synthetic-value-133","synthetic-value-134","synthetic-value-135"],"type":"string"},"synthetic_field_046":{"properties":{"synthetic_field_047":{"description":"Synthetic description 136.","type":"string"},"synthetic_field_048":{"synthetic_field_037":null,"description":"Synthetic description 137.","synthetic_field_021":["synthetic-value-138"],"anyOf":[{"type":"string"},{"type":"null"}]},"synthetic_field_049":{"synthetic_field_037":null,"description":"Synthetic description 139.","synthetic_field_021":["synthetic-value-140"],"anyOf":[{"type":"string"},{"type":"null"}]},"synthetic_field_050":{"synthetic_field_037":null,"description":"Synthetic description 141.","anyOf":[{"type":"string"},{"type":"null"}]},"synthetic_field_051":{"synthetic_field_037":null,"description":"Synthetic description 142.","anyOf":[{"type":"string"},{"type":"null"}]},"synthetic_field_052":{"synthetic_field_037":null,"description":"Synthetic description 143.","anyOf":[{"type":"string"},{"type":"null"}]},"synthetic_field_053":{"synthetic_field_037":null,"description":"Synthetic description 144.","anyOf":[{"type":"string"},{"type":"null"}]},"synthetic_field_054":{"description":"Synthetic description 145.","synthetic_field_021":["synthetic-value-146"],"type":"string"},"synthetic_field_055":{"synthetic_field_037":null,"description":"Synthetic description 147.","anyOf":[{"type":"string"},{"type":"null"}]},"synthetic_field_056":{"description":"Synthetic description 148.","type":"string"},"synthetic_field_057":{"synthetic_field_037":null,"description":"Synthetic description 149.","anyOf":[{"type":"string"},{"type":"null"}]},"synthetic_field_058":{"synthetic_field_037":null,"description":"Synthetic description 150.","anyOf":[{"type":"string"},{"type":"null"}]},"synthetic_field_059":{"synthetic_field_037":null,"description":"Synthetic description 151.","anyOf":[{"type":"string"},{"type":"null"}]}},"required":["synthetic_field_047","synthetic_field_054","synthetic_field_056"],"type":"object","additionalProperties":false}}},"description":"Synthetic description 152.","synthetic_field_008":null,"synthetic_field_009":false,"synthetic_field_010":false,"kind":"function","synthetic_field_011":null,"synthetic_field_012":null,"synthetic_field_013":false,"synthetic_field_014":null,"synthetic_field_015":null,"synthetic_field_016":null,"synthetic_field_017":null,"synthetic_field_018":null,"synthetic_field_019":"synthetic_id_001","synthetic_field_020":null},{"name":"synthetic_name_007","parameters_json_schema":{"additionalProperties":false,"synthetic_field_021":[{"synthetic_field_060":"synthetic-input-153"},{"synthetic_field_060":"synthetic-input-154"}],"properties":{"synthetic_field_060":{"description":"Synthetic description 155.","synthetic_field_021":["synthetic-value-156","synthetic-value-157"],"synthetic_field_036":1,"type":"string"}},"required":["synthetic_field_060"],"type":"object"},"description":"Synthetic description 158.","synthetic_field_008":null,"synthetic_field_009":false,"synthetic_field_010":false,"kind":"function","synthetic_field_011":null,"synthetic_field_012":null,"synthetic_field_013":false,"synthetic_field_014":null,"synthetic_field_015":null,"synthetic_field_016":null,"synthetic_field_017":null,"synthetic_field_018":null,"synthetic_field_019":"synthetic_id_001","synthetic_field_020":null},{"name":"synthetic_name_008","parameters_json_schema":{"additionalProperties":false,"synthetic_field_021":[{"synthetic_field_022":"synthetic_id_002","status":"synthetic-value-159"}],"properties":{"synthetic_field_022":{"description":"Synthetic description 160.","format":"synthetic-value-161","type":"string"},"status":{"description":"Synthetic description 162.","synthetic_field_021":["synthetic-value-163"],"anyOf":[{"synthetic_field_038":"synthetic-value-164"}]}},"required":["synthetic_field_022","synthetic_field_061"],"type":"object","synthetic_field_042":{"synthetic_field_062":{"enum":["synthetic-value-165","synthetic-value-166","synthetic-value-167"],"type":"string"}}},"description":"Synthetic description 168.","synthetic_field_008":null,"synthetic_field_009":true,"synthetic_field_010":false,"kind":"function","synthetic_field_011":null,"synthetic_field_012":null,"synthetic_field_013":false,"synthetic_field_014":null,"synthetic_field_015":null,"synthetic_field_016":null,"synthetic_field_017":null,"synthetic_field_018":null,"synthetic_field_019":"synthetic_id_001","synthetic_field_020":null},{"name":"synthetic_name_009","parameters_json_schema":{"additionalProperties":false,"properties":{},"type":"object"},"description":"Synthetic description 169.","synthetic_field_008":null,"synthetic_field_009":false,"synthetic_field_010":false,"kind":"function","synthetic_field_011":null,"synthetic_field_012":null,"synthetic_field_013":false,"synthetic_field_014":null,"synthetic_field_015":null,"synthetic_field_016":null,"synthetic_field_017":null,"synthetic_field_018":null,"synthetic_field_019":"synthetic_id_001","synthetic_field_020":null},{"name":"synthetic_name_010","parameters_json_schema":{"additionalProperties":false,"synthetic_field_021":[{"synthetic_field_063":[{"description":"Synthetic description 170.","synthetic_field_034":"synthetic-value-171"},{"description":"Synthetic description 172.","synthetic_field_034":"synthetic-value-173"}],"synthetic_field_022":"synthetic_id_002"}],"properties":{"synthetic_field_022":{"description":"Synthetic description 174.","format":"synthetic-value-175","type":"string"},"synthetic_field_063":{"description":"Synthetic description 176.","items":{"synthetic_field_038":"synthetic-value-177"},"synthetic_field_064":5,"type":"synthetic-value-178"}},"required":["synthetic_field_022","synthetic_field_063"],"type":"object","synthetic_field_042":{"synthetic_field_065":{"additionalProperties":false,"properties":{"synthetic_field_034":{"description":"Synthetic description 179.","synthetic_field_035":255,"type":"string"},"description":{"description":"Synthetic description 180.","type":"string"}},"required":["synthetic_field_034","synthetic_field_066"],"type":"object"}}},"description":"Synthetic description 181.","synthetic_field_008":null,"synthetic_field_009":false,"synthetic_field_010":false,"kind":"function","synthetic_field_011":null,"synthetic_field_012":null,"synthetic_field_013":false,"synthetic_field_014":null,"synthetic_field_015":null,"synthetic_field_016":null,"synthetic_field_017":null,"synthetic_field_018":null,"synthetic_field_019":"synthetic_id_001","synthetic_field_020":null},{"name":"synthetic_name_011","parameters_json_schema":{"additionalProperties":false,"synthetic_field_021":[{"synthetic_field_022":"synthetic_id_002","synthetic_field_067":[{"synthetic_field_068":"synthetic-value-182","required":true,"type":"boolean"},{"synthetic_field_068":"synthetic-value-183","required":false,"type":"text"}]}],"properties":{"synthetic_field_022":{"description":"Synthetic description 184.","format":"synthetic-value-185","type":"string"},"synthetic_field_067":{"description":"Synthetic description 186.","items":{"synthetic_field_038":"synthetic-value-187"},"type":"synthetic-value-188"}},"required":["synthetic_field_022","synthetic_field_067"],"type":"object","synthetic_field_042":{"synthetic_field_069":{"additionalProperties":false,"properties":{"synthetic_field_068":{"description":"Synthetic description 189.","synthetic_field_036":1,"type":"string"},"type":{"description":"Synthetic description 190.","enum":["text","boolean","synthetic-value-191"],"type":"string"},"required":{"description":"Synthetic description 192.","type":"boolean"}},"required":["synthetic_field_068","synthetic_field_070","synthetic_field_071"],"type":"object"}}},"description":"Synthetic description 193.","synthetic_field_008":null,"synthetic_field_009":false,"synthetic_field_010":false,"kind":"function","synthetic_field_011":null,"synthetic_field_012":null,"synthetic_field_013":false,"synthetic_field_014":null,"synthetic_field_015":null,"synthetic_field_016":null,"synthetic_field_017":null,"synthetic_field_018":null,"synthetic_field_019":"synthetic_id_001","synthetic_field_020":null},{"name":"synthetic_name_012","parameters_json_schema":{"additionalProperties":false,"properties":{"synthetic_field_072":{"description":"Synthetic description 194.","synthetic_field_036":1,"type":"string"}},"required":["synthetic_field_072"],"type":"object"},"description":"Synthetic description 195.","synthetic_field_008":null,"synthetic_field_009":false,"synthetic_field_010":false,"kind":"function","synthetic_field_011":null,"synthetic_field_012":null,"synthetic_field_013":false,"synthetic_field_014":null,"synthetic_field_015":null,"synthetic_field_016":null,"synthetic_field_017":null,"synthetic_field_018":null,"synthetic_field_019":"synthetic_id_001","synthetic_field_020":null}],"synthetic_field_073":[],"synthetic_field_074":{"synthetic_field_075":"synthetic-value-196","synthetic_field_076":"synthetic-value-197","synthetic_field_077":"synthetic-value-198","synthetic_field_078":"synthetic-value-199","synthetic_field_079":"synthetic-value-200","synthetic_field_080":"synthetic-value-201","synthetic_field_081":"synthetic-value-202","synthetic_field_082":"synthetic-value-203","synthetic_field_083":"synthetic-value-204","synthetic_field_084":"synthetic-value-205","synthetic_field_085":"synthetic-value-206"},"synthetic_field_086":[],"synthetic_field_087":"synthetic-output-207","synthetic_field_088":null,"synthetic_field_089":[],"synthetic_field_090":null,"synthetic_field_091":true,"synthetic_field_092":false,"synthetic_field_093":[{"content":"synthetic-value-208","synthetic_field_094":false,"synthetic_field_095":"synthetic-value-209"}],"thinking":null}',+ },+ },+ {+ key: "gen_ai.tool.definitions",+ value: {+ stringValue:+ '[{"type":"function","name":"synthetic_tool_001","description":"Synthetic description 210.","parameters":{"additionalProperties":false,"properties":{"url":{"type":"string"}},"required":["synthetic_field_007"],"type":"object"}},{"type":"function","name":"synthetic_tool_002","description":"Synthetic description 211.","parameters":{"additionalProperties":false,"properties":{},"type":"object"}},{"type":"function","name":"synthetic_tool_003","description":"Synthetic description 212.","parameters":{"additionalProperties":false,"synthetic_field_021":[{"synthetic_field_022":"synthetic_id_002"}],"properties":{"synthetic_field_022":{"description":"Synthetic description 213.","format":"synthetic-value-214","type":"string"}},"required":["synthetic_field_022"],"type":"object"}},{"type":"function","name":"synthetic_tool_004","description":"Synthetic description 215.","parameters":{"additionalProperties":false,"synthetic_field_021":[{"synthetic_field_023":"synthetic-value-216","synthetic_field_024":"synthetic-value-217","synthetic_field_025":"synthetic-value-218","synthetic_field_026":"synthetic-value-219","synthetic_field_027":"synthetic-value-220","synthetic_field_028":"synthetic-value-221","synthetic_field_029":"synthetic-value-222","synthetic_field_030":"synthetic-value-223","synthetic_field_031":85000,"synthetic_field_032":65000,"synthetic_field_033":"synthetic-value-224","synthetic_field_034":"synthetic-value-225"}],"properties":{"synthetic_field_034":{"description":"Synthetic description 226.","synthetic_field_021":["synthetic-value-227"],"synthetic_field_035":255,"synthetic_field_036":1,"type":"string"},"synthetic_field_029":{"synthetic_field_037":null,"description":"Synthetic description 228.","anyOf":[{"type":"string"},{"type":"null"}]},"synthetic_field_026":{"synthetic_field_037":null,"description":"Synthetic description 229.","anyOf":[{"type":"string"},{"type":"null"}]},"synthetic_field_027":{"synthetic_field_037":null,"description":"Synthetic description 230.","anyOf":[{"type":"string"},{"type":"null"}]},"synthetic_field_023":{"synthetic_field_037":null,"description":"Synthetic description 231.","anyOf":[{"type":"string"},{"type":"null"}]},"synthetic_field_024":{"synthetic_field_037":null,"description":"Synthetic description 232.","anyOf":[{"type":"string"},{"type":"null"}]},"synthetic_field_025":{"synthetic_field_037":null,"description":"Synthetic description 233.","synthetic_field_021":["synthetic-value-234"],"anyOf":[{"synthetic_field_038":"synthetic-value-235"},{"type":"null"}]},"synthetic_field_028":{"synthetic_field_037":null,"description":"Synthetic description 236.","synthetic_field_021":["synthetic-value-237"],"anyOf":[{"synthetic_field_038":"synthetic-value-238"},{"type":"null"}]},"synthetic_field_033":{"synthetic_field_037":null,"description":"Synthetic description 239.","anyOf":[{"type":"string"},{"type":"null"}]},"synthetic_field_039":{"synthetic_field_037":null,"description":"Synthetic description 240.","anyOf":[{"items":{"synthetic_field_038":"synthetic-value-241"},"type":"synthetic-value-242"},{"type":"null"}]},"synthetic_field_030":{"synthetic_field_037":null,"description":"Synthetic description 243.","synthetic_field_021":["synthetic-value-244"],"anyOf":[{"synthetic_field_038":"synthetic-value-245"},{"type":"null"}]},"synthetic_field_032":{"synthetic_field_037":null,"description":"Synthetic description 246.","anyOf":[{"type":"integer"},{"type":"null"}]},"synthetic_field_031":{"synthetic_field_037":null,"description":"Synthetic description 247.","anyOf":[{"type":"integer"},{"type":"null"}]},"synthetic_field_040":{"synthetic_field_037":null,"description":"Synthetic description 248.","anyOf":[{"type":"integer"},{"type":"null"}]},"synthetic_field_041":{"synthetic_field_037":null,"description":"Synthetic description 249.","anyOf":[{"type":"integer"},{"type":"null"}]}},"required":["synthetic_field_034"],"type":"object","synthetic_field_042":{"synthetic_field_043":{"enum":["synthetic-value-250","synthetic-value-251","synthetic-value-252","synthetic-value-253","synthetic-value-254","synthetic-value-255","synthetic-value-256","synthetic-value-257","synthetic-value-258","synthetic-value-259"],"type":"string"},"synthetic_field_044":{"enum":["synthetic-value-260","synthetic-value-261","synthetic-value-262"],"type":"string"},"synthetic_field_045":{"enum":["synthetic-value-263","synthetic-value-264","synthetic-value-265"],"type":"string"},"synthetic_field_046":{"properties":{"synthetic_field_047":{"description":"Synthetic description 266.","type":"string"},"synthetic_field_048":{"synthetic_field_037":null,"description":"Synthetic description 267.","synthetic_field_021":["synthetic-value-268"],"anyOf":[{"type":"string"},{"type":"null"}]},"synthetic_field_049":{"synthetic_field_037":null,"description":"Synthetic description 269.","synthetic_field_021":["synthetic-value-270"],"anyOf":[{"type":"string"},{"type":"null"}]},"synthetic_field_050":{"synthetic_field_037":null,"description":"Synthetic description 271.","anyOf":[{"type":"string"},{"type":"null"}]},"synthetic_field_051":{"synthetic_field_037":null,"description":"Synthetic description 272.","anyOf":[{"type":"string"},{"type":"null"}]},"synthetic_field_052":{"synthetic_field_037":null,"description":"Synthetic description 273.","anyOf":[{"type":"string"},{"type":"null"}]},"synthetic_field_053":{"synthetic_field_037":null,"description":"Synthetic description 274.","anyOf":[{"type":"string"},{"type":"null"}]},"synthetic_field_054":{"description":"Synthetic description 275.","synthetic_field_021":["synthetic-value-276"],"type":"string"},"synthetic_field_055":{"synthetic_field_037":null,"description":"Synthetic description 277.","anyOf":[{"type":"string"},{"type":"null"}]},"synthetic_field_056":{"description":"Synthetic description 278.","type":"string"},"synthetic_field_057":{"synthetic_field_037":null,"description":"Synthetic description 279.","anyOf":[{"type":"string"},{"type":"null"}]},"synthetic_field_058":{"synthetic_field_037":null,"description":"Synthetic description 280.","anyOf":[{"type":"string"},{"type":"null"}]},"synthetic_field_059":{"synthetic_field_037":null,"description":"Synthetic description 281.","anyOf":[{"type":"string"},{"type":"null"}]}},"required":["synthetic_field_047","synthetic_field_054","synthetic_field_056"],"type":"object","additionalProperties":false}}}},{"type":"function","name":"synthetic_tool_005","description":"Synthetic description 282.","parameters":{"additionalProperties":false,"synthetic_field_021":[{"synthetic_field_022":"synthetic_id_002","synthetic_field_028":"synthetic-value-283","synthetic_field_031":90000,"synthetic_field_032":70000,"synthetic_field_033":"synthetic-value-284"}],"properties":{"synthetic_field_022":{"description":"Synthetic description 285.","format":"synthetic-value-286","type":"string"},"synthetic_field_034":{"synthetic_field_037":null,"description":"Synthetic description 287.","synthetic_field_021":["synthetic-value-288"],"anyOf":[{"synthetic_field_035":255,"synthetic_field_036":1,"type":"string"},{"type":"null"}]},"synthetic_field_029":{"synthetic_field_037":null,"description":"Synthetic description 289.","anyOf":[{"type":"string"},{"type":"null"}]},"synthetic_field_026":{"synthetic_field_037":null,"description":"Synthetic description 290.","anyOf":[{"type":"string"},{"type":"null"}]},"synthetic_field_027":{"synthetic_field_037":null,"description":"Synthetic description 291.","anyOf":[{"type":"string"},{"type":"null"}]},"synthetic_field_023":{"synthetic_field_037":null,"description":"Synthetic description 292.","anyOf":[{"type":"string"},{"type":"null"}]},"synthetic_field_024":{"synthetic_field_037":null,"description":"Synthetic description 293.","anyOf":[{"type":"string"},{"type":"null"}]},"synthetic_field_025":{"synthetic_field_037":null,"description":"Synthetic description 294.","synthetic_field_021":["synthetic-value-295"],"anyOf":[{"synthetic_field_038":"synthetic-value-296"},{"type":"null"}]},"synthetic_field_028":{"synthetic_field_037":null,"description":"Synthetic description 297.","synthetic_field_021":["synthetic-value-298"],"anyOf":[{"synthetic_field_038":"synthetic-value-299"},{"type":"null"}]},"synthetic_field_033":{"synthetic_field_037":null,"description":"Synthetic description 300.","anyOf":[{"type":"string"},{"type":"null"}]},"synthetic_field_039":{"synthetic_field_037":null,"description":"Synthetic description 301.","anyOf":[{"items":{"synthetic_field_038":"synthetic-value-302"},"type":"synthetic-value-303"},{"type":"null"}]},"synthetic_field_030":{"synthetic_field_037":null,"description":"Synthetic description 304.","synthetic_field_021":["synthetic-value-305"],"anyOf":[{"synthetic_field_038":"synthetic-value-306"},{"type":"null"}]},"synthetic_field_032":{"synthetic_field_037":null,"description":"Synthetic description 307.","anyOf":[{"type":"integer"},{"type":"null"}]},"synthetic_field_031":{"synthetic_field_037":null,"description":"Synthetic description 308.","anyOf":[{"type":"integer"},{"type":"null"}]},"synthetic_field_040":{"synthetic_field_037":null,"description":"Synthetic description 309.","anyOf":[{"type":"integer"},{"type":"null"}]},"synthetic_field_041":{"synthetic_field_037":null,"description":"Synthetic description 310.","anyOf":[{"type":"integer"},{"type":"null"}]}},"required":["synthetic_field_022"],"type":"object","synthetic_field_042":{"synthetic_field_043":{"enum":["synthetic-value-311","synthetic-value-312","synthetic-value-313","synthetic-value-314","synthetic-value-315","synthetic-value-316","synthetic-value-317","synthetic-value-318","synthetic-value-319","synthetic-value-320"],"type":"string"},"synthetic_field_044":{"enum":["synthetic-value-321","synthetic-value-322","synthetic-value-323"],"type":"string"},"synthetic_field_045":{"enum":["synthetic-value-324","synthetic-value-325","synthetic-value-326"],"type":"string"},"synthetic_field_046":{"properties":{"synthetic_field_047":{"description":"Synthetic description 327.","type":"string"},"synthetic_field_048":{"synthetic_field_037":null,"description":"Synthetic description 328.","synthetic_field_021":["synthetic-value-329"],"anyOf":[{"type":"string"},{"type":"null"}]},"synthetic_field_049":{"synthetic_field_037":null,"description":"Synthetic description 330.","synthetic_field_021":["synthetic-value-331"],"anyOf":[{"type":"string"},{"type":"null"}]},"synthetic_field_050":{"synthetic_field_037":null,"description":"Synthetic description 332.","anyOf":[{"type":"string"},{"type":"null"}]},"synthetic_field_051":{"synthetic_field_037":null,"description":"Synthetic description 333.","anyOf":[{"type":"string"},{"type":"null"}]},"synthetic_field_052":{"synthetic_field_037":null,"description":"Synthetic description 334.","anyOf":[{"type":"string"},{"type":"null"}]},"synthetic_field_053":{"synthetic_field_037":null,"description":"Synthetic description 335.","anyOf":[{"type":"string"},{"type":"null"}]},"synthetic_field_054":{"description":"Synthetic description 336.","synthetic_field_021":["synthetic-value-337"],"type":"string"},"synthetic_field_055":{"synthetic_field_037":null,"description":"Synthetic description 338.","anyOf":[{"type":"string"},{"type":"null"}]},"synthetic_field_056":{"description":"Synthetic description 339.","type":"string"},"synthetic_field_057":{"synthetic_field_037":null,"description":"Synthetic description 340.","anyOf":[{"type":"string"},{"type":"null"}]},"synthetic_field_058":{"synthetic_field_037":null,"description":"Synthetic description 341.","anyOf":[{"type":"string"},{"type":"null"}]},"synthetic_field_059":{"synthetic_field_037":null,"description":"Synthetic description 342.","anyOf":[{"type":"string"},{"type":"null"}]}},"required":["synthetic_field_047","synthetic_field_054","synthetic_field_056"],"type":"object","additionalProperties":false}}}},{"type":"function","name":"synthetic_tool_006","description":"Synthetic description 343.","parameters":{"additionalProperties":false,"synthetic_field_021":[{"synthetic_field_060":"synthetic-input-344"},{"synthetic_field_060":"synthetic-input-345"}],"properties":{"synthetic_field_060":{"description":"Synthetic description 346.","synthetic_field_021":["synthetic-value-347","synthetic-value-348"],"synthetic_field_036":1,"type":"string"}},"required":["synthetic_field_060"],"type":"object"}},{"type":"function","name":"synthetic_tool_007","description":"Synthetic description 349.","parameters":{"additionalProperties":false,"synthetic_field_021":[{"synthetic_field_022":"synthetic_id_002","status":"synthetic-value-350"}],"properties":{"synthetic_field_022":{"description":"Synthetic description 351.","format":"synthetic-value-352","type":"string"},"status":{"description":"Synthetic description 353.","synthetic_field_021":["synthetic-value-354"],"anyOf":[{"synthetic_field_038":"synthetic-value-355"}]}},"required":["synthetic_field_022","synthetic_field_061"],"type":"object","synthetic_field_042":{"synthetic_field_062":{"enum":["synthetic-value-356","synthetic-value-357","synthetic-value-358"],"type":"string"}}}},{"type":"function","name":"synthetic_tool_008","description":"Synthetic description 359.","parameters":{"additionalProperties":false,"properties":{},"type":"object"}},{"type":"function","name":"synthetic_tool_009","description":"Synthetic description 360.","parameters":{"additionalProperties":false,"synthetic_field_021":[{"synthetic_field_063":[{"description":"Synthetic description 361.","synthetic_field_034":"synthetic-value-362"},{"description":"Synthetic description 363.","synthetic_field_034":"synthetic-value-364"}],"synthetic_field_022":"synthetic_id_002"}],"properties":{"synthetic_field_022":{"description":"Synthetic description 365.","format":"synthetic-value-366","type":"string"},"synthetic_field_063":{"description":"Synthetic description 367.","items":{"synthetic_field_038":"synthetic-value-368"},"synthetic_field_064":5,"type":"synthetic-value-369"}},"required":["synthetic_field_022","synthetic_field_063"],"type":"object","synthetic_field_042":{"synthetic_field_065":{"additionalProperties":false,"properties":{"synthetic_field_034":{"description":"Synthetic description 370.","synthetic_field_035":255,"type":"string"},"description":{"description":"Synthetic description 371.","type":"string"}},"required":["synthetic_field_034","synthetic_field_066"],"type":"object"}}}},{"type":"function","name":"synthetic_tool_010","description":"Synthetic description 372.","parameters":{"additionalProperties":false,"synthetic_field_021":[{"synthetic_field_022":"synthetic_id_002","synthetic_field_067":[{"synthetic_field_068":"synthetic-value-373","required":true,"type":"boolean"},{"synthetic_field_068":"synthetic-value-374","required":false,"type":"text"}]}],"properties":{"synthetic_field_022":{"description":"Synthetic description 375.","format":"synthetic-value-376","type":"string"},"synthetic_field_067":{"description":"Synthetic description 377.","items":{"synthetic_field_038":"synthetic-value-378"},"type":"synthetic-value-379"}},"required":["synthetic_field_022","synthetic_field_067"],"type":"object","synthetic_field_042":{"synthetic_field_069":{"additionalProperties":false,"properties":{"synthetic_field_068":{"description":"Synthetic description 380.","synthetic_field_036":1,"type":"string"},"type":{"description":"Synthetic description 381.","enum":["text","boolean","synthetic-value-382"],"type":"string"},"required":{"description":"Synthetic description 383.","type":"boolean"}},"required":["synthetic_field_068","synthetic_field_070","synthetic_field_071"],"type":"object"}}}},{"type":"function","name":"synthetic_tool_011","description":"Synthetic description 384.","parameters":{"additionalProperties":false,"properties":{"synthetic_field_072":{"description":"Synthetic description 385.","synthetic_field_036":1,"type":"string"}},"required":["synthetic_field_072"],"type":"object"}}]',+ },+ },+ {+ key: "gen_ai.request.max_tokens",+ value: {+ intValue: "synthetic-value-386",+ },+ },+ {+ key: "synthetic.metadata.008",+ value: {+ stringValue: "synthetic-value-387",+ },+ },+ {+ key: "synthetic.metadata.009",+ value: {+ stringValue: "synthetic-value-388",+ },+ },+ {+ key: "langfuse.trace.name",+ value: {+ stringValue: "synthetic-value-389",+ },+ },+ {+ key: "langfuse.environment",+ value: {+ stringValue: "synthetic-value-390",+ },+ },+ {+ key: "gen_ai.input.messages",+ value: {+ stringValue:+ '[{"role":"user","parts":[{"type":"text","content":"Synthetic user request 391."}]}]',+ },+ },+ {+ key: "gen_ai.output.messages",+ value: {+ stringValue:+ '[{"role":"assistant","parts":[{"type":"tool_call","id":"call_001","name":"synthetic_tool_011","arguments":"{\\"synthetic_field_072\\":\\"Synthetic assistant response 392.\\"}"}],"finish_reason":"tool_call"}]',+ },+ },+ {+ key: "gen_ai.system_instructions",+ value: {+ stringValue:+ '[{"type":"text","content":"synthetic-value-393"}]',+ },+ },+ {+ key: "synthetic.metadata.010",+ value: {+ stringValue:+ '{"type":"object","properties":{"gen_ai.input.messages":{"type":"synthetic-value-394"},"gen_ai.output.messages":{"type":"synthetic-value-395"},"gen_ai.system_instructions":{"type":"synthetic-value-396"},"synthetic_field_096":{"type":"object"}}}',+ },+ },+ {+ key: "gen_ai.usage.input_tokens",+ value: {+ intValue: "synthetic-value-397",+ },+ },+ {+ key: "gen_ai.usage.output_tokens",+ value: {+ intValue: "synthetic-value-398",+ },+ },+ {+ key: "gen_ai.usage.details.accepted_prediction_tokens",+ value: {+ intValue: "synthetic-value-399",+ },+ },+ {+ key: "gen_ai.usage.details.audio_tokens",+ value: {+ intValue: "synthetic-value-400",+ },+ },+ {+ key: "gen_ai.usage.details.reasoning_tokens",+ value: {+ intValue: "synthetic-value-401",+ },+ },+ {+ key: "gen_ai.usage.details.rejected_prediction_tokens",+ value: {+ intValue: "synthetic-value-402",+ },+ },+ {+ key: "gen_ai.response.model",+ value: {+ stringValue: "synthetic-value-403",+ },+ },+ {+ key: "synthetic.metadata.011",+ value: {+ doubleValue: 0.005547,+ },+ },+ {+ key: "gen_ai.response.id",+ value: {+ stringValue: "synthetic-value-404",+ },+ },+ {+ key: "gen_ai.response.finish_reasons",+ value: {+ arrayValue: {+ values: [+ {+ stringValue: "synthetic-value-405",+ },+ ],+ },+ },+ },+ {+ key: "gen_ai.client.operation.time_to_first_chunk",+ value: {+ doubleValue: 1.18922124899996,+ },+ },+ ],+ status: {},+ },+ ],+ },+ resourceAttributes: {+ "telemetry.sdk.language": "python",+ "telemetry.sdk.name": "opentelemetry",+ "telemetry.sdk.version": "1.39.1",+ "synthetic.metadata.001": "synthetic-value-008",+ "service.version": "synthetic-value-009",+ "synthetic.metadata.002": "synthetic-value-010",+ "synthetic.metadata.003": "synthetic-value-011",+ "synthetic.metadata.004": "synthetic-value-012",+ "synthetic.metadata.005": "synthetic-value-013",+ "service.name": "synthetic-service",+ "telemetry.auto.version": "synthetic-value-014",+ },+ },+ spanIO: {+ input: {+ messages: [+ {+ role: "system",+ content: "synthetic-value-393",+ },+ {+ role: "user",+ parts: [+ {+ type: "text",+ content: "Synthetic user request 391.",+ },+ ],+ },+ ],+ tools: [+ {+ type: "function",+ name: "synthetic_tool_001",+ description: "Synthetic description 210.",+ parameters: {+ additionalProperties: false,+ properties: {+ url: {+ type: "string",+ },+ },+ required: ["synthetic_field_007"],+ type: "object",+ },+ },+ {+ type: "function",+ name: "synthetic_tool_002",+ description: "Synthetic description 211.",+ parameters: {+ additionalProperties: false,+ properties: {},+ type: "object",+ },+ },+ {+ type: "function",+ name: "synthetic_tool_003",+ description: "Synthetic description 212.",+ parameters: {+ additionalProperties: false,+ synthetic_field_021: [+ {+ synthetic_field_022: "synthetic_id_002",+ },+ ],+ properties: {+ synthetic_field_022: {+ description: "Synthetic description 213.",+ format: "synthetic-value-214",+ type: "string",+ },+ },+ required: ["synthetic_field_022"],+ type: "object",+ },+ },+ {+ type: "function",+ name: "synthetic_tool_004",+ description: "Synthetic description 215.",+ parameters: {+ additionalProperties: false,+ synthetic_field_021: [+ {+ synthetic_field_023: "synthetic-value-216",+ synthetic_field_024: "synthetic-value-217",+ synthetic_field_025: "synthetic-value-218",+ synthetic_field_026: "synthetic-value-219",+ synthetic_field_027: "synthetic-value-220",+ synthetic_field_028: "synthetic-value-221",+ synthetic_field_029: "synthetic-value-222",+ synthetic_field_030: "synthetic-value-223",+ synthetic_field_031: 85000,+ synthetic_field_032: 65000,+ synthetic_field_033: "synthetic-value-224",+ synthetic_field_034: "synthetic-value-225",+ },+ ],+ properties: {+ synthetic_field_034: {+ description: "Synthetic description 226.",+ synthetic_field_021: ["synthetic-value-227"],+ synthetic_field_035: 255,+ synthetic_field_036: 1,+ type: "string",+ },+ synthetic_field_029: {+ synthetic_field_037: null,+ description: "Synthetic description 228.",+ anyOf: [+ {+ type: "string",+ },+ {+ type: "null",+ },+ ],+ },+ synthetic_field_026: {+ synthetic_field_037: null,+ description: "Synthetic description 229.",+ anyOf: [+ {+ type: "string",+ },+ {+ type: "null",+ },+ ],+ },+ synthetic_field_027: {+ synthetic_field_037: null,+ description: "Synthetic description 230.",+ anyOf: [+ {+ type: "string",+ },+ {+ type: "null",+ },+ ],+ },+ synthetic_field_023: {+ synthetic_field_037: null,+ description: "Synthetic description 231.",+ anyOf: [+ {+ type: "string",+ },+ {+ type: "null",+ },+ ],+ },+ synthetic_field_024: {+ synthetic_field_037: null,+ description: "Synthetic description 232.",+ anyOf: [+ {+ type: "string",+ },+ {+ type: "null",+ },+ ],+ },+ synthetic_field_025: {+ synthetic_field_037: null,+ description: "Synthetic description 233.",+ synthetic_field_021: ["synthetic-value-234"],+ anyOf: [+ {+ synthetic_field_038: "synthetic-value-235",+ },+ {+ type: "null",+ },+ ],+ },+ synthetic_field_028: {+ synthetic_field_037: null,+ description: "Synthetic description 236.",+ synthetic_field_021: ["synthetic-value-237"],+ anyOf: [+ {+ synthetic_field_038: "synthetic-value-238",+ },+ {+ type: "null",+ },+ ],+ },+ synthetic_field_033: {+ synthetic_field_037: null,+ description: "Synthetic description 239.",+ anyOf: [+ {+ type: "string",+ },+ {+ type: "null",+ },+ ],+ },+ synthetic_field_039: {+ synthetic_field_037: null,+ description: "Synthetic description 240.",+ anyOf: [+ {+ items: {+ synthetic_field_038: "synthetic-value-241",+ },+ type: "synthetic-value-242",+ },+ {+ type: "null",+ },+ ],+ },+ synthetic_field_030: {+ synthetic_field_037: null,+ description: "Synthetic description 243.",+ synthetic_field_021: ["synthetic-value-244"],+ anyOf: [+ {+ synthetic_field_038: "synthetic-value-245",+ },+ {+ type: "null",+ },+ ],+ },+ synthetic_field_032: {+ synthetic_field_037: null,+ description: "Synthetic description 246.",+ anyOf: [+ {+ type: "integer",+ },+ {+ type: "null",+ },+ ],+ },+ synthetic_field_031: {+ synthetic_field_037: null,+ description: "Synthetic description 247.",+ anyOf: [+ {+ type: "integer",+ },+ {+ type: "null",+ },+ ],+ },+ synthetic_field_040: {+ synthetic_field_037: null,+ description: "Synthetic description 248.",+ anyOf: [+ {+ type: "integer",+ },+ {+ type: "null",+ },+ ],+ },+ synthetic_field_041: {+ synthetic_field_037: null,+ description: "Synthetic description 249.",+ anyOf: [+ {+ type: "integer",+ },+ {+ type: "null",+ },+ ],+ },+ },+ required: ["synthetic_field_034"],+ type: "object",+ synthetic_field_042: {+ synthetic_field_043: {+ enum: [+ "synthetic-value-250",+ "synthetic-value-251",+ "synthetic-value-252",+ "synthetic-value-253",+ "synthetic-value-254",+ "synthetic-value-255",+ "synthetic-value-256",+ "synthetic-value-257",+ "synthetic-value-258",+ "synthetic-value-259",+ ],+ type: "string",+ },+ synthetic_field_044: {+ enum: [+ "synthetic-value-260",+ "synthetic-value-261",+ "synthetic-value-262",+ ],+ type: "string",+ },+ synthetic_field_045: {+ enum: [+ "synthetic-value-263",+ "synthetic-value-264",+ "synthetic-value-265",+ ],+ type: "string",+ },+ synthetic_field_046: {+ properties: {+ synthetic_field_047: {+ description: "Synthetic description 266.",+ type: "string",+ },+ synthetic_field_048: {+ synthetic_field_037: null,+ description: "Synthetic description 267.",+ synthetic_field_021: ["synthetic-value-268"],+ anyOf: [+ {+ type: "string",+ },+ {+ type: "null",+ },+ ],+ },+ synthetic_field_049: {+ synthetic_field_037: null,+ description: "Synthetic description 269.",+ synthetic_field_021: ["synthetic-value-270"],+ anyOf: [+ {+ type: "string",+ },+ {+ type: "null",+ },+ ],+ },+ synthetic_field_050: {+ synthetic_field_037: null,+ description: "Synthetic description 271.",+ anyOf: [+ {+ type: "string",+ },+ {+ type: "null",+ },+ ],+ },+ synthetic_field_051: {+ synthetic_field_037: null,+ description: "Synthetic description 272.",+ anyOf: [+ {+ type: "string",+ },+ {+ type: "null",+ },+ ],+ },+ synthetic_field_052: {+ synthetic_field_037: null,+ description: "Synthetic description 273.",+ anyOf: [+ {+ type: "string",+ },+ {+ type: "null",+ },+ ],+ },+ synthetic_field_053: {+ synthetic_field_037: null,+ description: "Synthetic description 274.",+ anyOf: [+ {+ type: "string",+ },+ {+ type: "null",+ },+ ],+ },+ synthetic_field_054: {+ description: "Synthetic description 275.",+ synthetic_field_021: ["synthetic-value-276"],+ type: "string",+ },+ synthetic_field_055: {+ synthetic_field_037: null,+ description: "Synthetic description 277.",+ anyOf: [+ {+ type: "string",+ },+ {+ type: "null",+ },+ ],+ },+ synthetic_field_056: {+ description: "Synthetic description 278.",+ type: "string",+ },+ synthetic_field_057: {+ synthetic_field_037: null,+ description: "Synthetic description 279.",+ anyOf: [+ {+ type: "string",+ },+ {+ type: "null",+ },+ ],+ },+ synthetic_field_058: {+ synthetic_field_037: null,+ description: "Synthetic description 280.",+ anyOf: [+ {+ type: "string",+ },+ {+ type: "null",+ },+ ],+ },+ synthetic_field_059: {+ synthetic_field_037: null,+ description: "Synthetic description 281.",+ anyOf: [+ {+ type: "string",+ },+ {+ type: "null",+ },+ ],+ },+ },+ required: [+ "synthetic_field_047",+ "synthetic_field_054",+ "synthetic_field_056",+ ],+ type: "object",+ additionalProperties: false,+ },+ },+ },+ },+ {+ type: "function",+ name: "synthetic_tool_005",+ description: "Synthetic description 282.",+ parameters: {+ additionalProperties: false,+ synthetic_field_021: [+ {+ synthetic_field_022: "synthetic_id_002",+ synthetic_field_028: "synthetic-value-283",+ synthetic_field_031: 90000,+ synthetic_field_032: 70000,+ synthetic_field_033: "synthetic-value-284",+ },+ ],+ properties: {+ synthetic_field_022: {+ description: "Synthetic description 285.",+ format: "synthetic-value-286",+ type: "string",+ },+ synthetic_field_034: {+ synthetic_field_037: null,+ description: "Synthetic description 287.",+ synthetic_field_021: ["synthetic-value-288"],+ anyOf: [+ {+ synthetic_field_035: 255,+ synthetic_field_036: 1,+ type: "string",+ },+ {+ type: "null",+ },+ ],+ },+ synthetic_field_029: {+ synthetic_field_037: null,+ description: "Synthetic description 289.",+ anyOf: [+ {+ type: "string",+ },+ {+ type: "null",+ },+ ],+ },+ synthetic_field_026: {+ synthetic_field_037: null,+ description: "Synthetic description 290.",+ anyOf: [+ {+ type: "string",+ },+ {+ type: "null",+ },+ ],+ },+ synthetic_field_027: {+ synthetic_field_037: null,+ description: "Synthetic description 291.",+ anyOf: [+ {+ type: "string",+ },+ {+ type: "null",+ },+ ],+ },+ synthetic_field_023: {+ synthetic_field_037: null,+ description: "Synthetic description 292.",+ anyOf: [+ {+ type: "string",+ },+ {+ type: "null",+ },+ ],+ },+ synthetic_field_024: {+ synthetic_field_037: null,+ description: "Synthetic description 293.",+ anyOf: [+ {+ type: "string",+ },+ {+ type: "null",+ },+ ],+ },+ synthetic_field_025: {+ synthetic_field_037: null,+ description: "Synthetic description 294.",+ synthetic_field_021: ["synthetic-value-295"],+ anyOf: [+ {+ synthetic_field_038: "synthetic-value-296",+ },+ {+ type: "null",+ },+ ],+ },+ synthetic_field_028: {+ synthetic_field_037: null,+ description: "Synthetic description 297.",+ synthetic_field_021: ["synthetic-value-298"],+ anyOf: [+ {+ synthetic_field_038: "synthetic-value-299",+ },+ {+ type: "null",+ },+ ],+ },+ synthetic_field_033: {+ synthetic_field_037: null,+ description: "Synthetic description 300.",+ anyOf: [+ {+ type: "string",+ },+ {+ type: "null",+ },+ ],+ },+ synthetic_field_039: {+ synthetic_field_037: null,+ description: "Synthetic description 301.",+ anyOf: [+ {+ items: {+ synthetic_field_038: "synthetic-value-302",+ },+ type: "synthetic-value-303",+ },+ {+ type: "null",+ },+ ],+ },+ synthetic_field_030: {+ synthetic_field_037: null,+ description: "Synthetic description 304.",+ synthetic_field_021: ["synthetic-value-305"],+ anyOf: [+ {+ synthetic_field_038: "synthetic-value-306",+ },+ {+ type: "null",+ },+ ],+ },+ synthetic_field_032: {+ synthetic_field_037: null,+ description: "Synthetic description 307.",+ anyOf: [+ {+ type: "integer",+ },+ {+ type: "null",+ },+ ],+ },+ synthetic_field_031: {+ synthetic_field_037: null,+ description: "Synthetic description 308.",+ anyOf: [+ {+ type: "integer",+ },+ {+ type: "null",+ },+ ],+ },+ synthetic_field_040: {+ synthetic_field_037: null,+ description: "Synthetic description 309.",+ anyOf: [+ {+ type: "integer",+ },+ {+ type: "null",+ },+ ],+ },+ synthetic_field_041: {+ synthetic_field_037: null,+ description: "Synthetic description 310.",+ anyOf: [+ {+ type: "integer",+ },+ {+ type: "null",+ },+ ],+ },+ },+ required: ["synthetic_field_022"],+ type: "object",+ synthetic_field_042: {+ synthetic_field_043: {+ enum: [+ "synthetic-value-311",+ "synthetic-value-312",+ "synthetic-value-313",+ "synthetic-value-314",+ "synthetic-value-315",+ "synthetic-value-316",+ "synthetic-value-317",+ "synthetic-value-318",+ "synthetic-value-319",+ "synthetic-value-320",+ ],+ type: "string",+ },+ synthetic_field_044: {+ enum: [+ "synthetic-value-321",+ "synthetic-value-322",+ "synthetic-value-323",+ ],+ type: "string",+ },+ synthetic_field_045: {+ enum: [+ "synthetic-value-324",+ "synthetic-value-325",+ "synthetic-value-326",+ ],+ type: "string",+ },+ synthetic_field_046: {+ properties: {+ synthetic_field_047: {+ description: "Synthetic description 327.",+ type: "string",+ },+ synthetic_field_048: {+ synthetic_field_037: null,+ description: "Synthetic description 328.",+ synthetic_field_021: ["synthetic-value-329"],+ anyOf: [+ {+ type: "string",+ },+ {+ type: "null",+ },+ ],+ },+ synthetic_field_049: {+ synthetic_field_037: null,+ description: "Synthetic description 330.",+ synthetic_field_021: ["synthetic-value-331"],+ anyOf: [+ {+ type: "string",+ },+ {+ type: "null",+ },+ ],+ },+ synthetic_field_050: {+ synthetic_field_037: null,+ description: "Synthetic description 332.",+ anyOf: [+ {+ type: "string",+ },+ {+ type: "null",+ },+ ],+ },+ synthetic_field_051: {+ synthetic_field_037: null,+ description: "Synthetic description 333.",+ anyOf: [+ {+ type: "string",+ },+ {+ type: "null",+ },+ ],+ },+ synthetic_field_052: {+ synthetic_field_037: null,+ description: "Synthetic description 334.",+ anyOf: [+ {+ type: "string",+ },+ {+ type: "null",+ },+ ],+ },+ synthetic_field_053: {+ synthetic_field_037: null,+ description: "Synthetic description 335.",+ anyOf: [+ {+ type: "string",+ },+ {+ type: "null",+ },+ ],+ },+ synthetic_field_054: {+ description: "Synthetic description 336.",+ synthetic_field_021: ["synthetic-value-337"],+ type: "string",+ },+ synthetic_field_055: {+ synthetic_field_037: null,+ description: "Synthetic description 338.",+ anyOf: [+ {+ type: "string",+ },+ {+ type: "null",+ },+ ],+ },+ synthetic_field_056: {+ description: "Synthetic description 339.",+ type: "string",+ },+ synthetic_field_057: {+ synthetic_field_037: null,+ description: "Synthetic description 340.",+ anyOf: [+ {+ type: "string",+ },+ {+ type: "null",+ },+ ],+ },+ synthetic_field_058: {+ synthetic_field_037: null,+ description: "Synthetic description 341.",+ anyOf: [+ {+ type: "string",+ },+ {+ type: "null",+ },+ ],+ },+ synthetic_field_059: {+ synthetic_field_037: null,+ description: "Synthetic description 342.",+ anyOf: [+ {+ type: "string",+ },+ {+ type: "null",+ },+ ],+ },+ },+ required: [+ "synthetic_field_047",+ "synthetic_field_054",+ "synthetic_field_056",+ ],+ type: "object",+ additionalProperties: false,+ },+ },+ },+ },+ {+ type: "function",+ name: "synthetic_tool_006",+ description: "Synthetic description 343.",+ parameters: {+ additionalProperties: false,+ synthetic_field_021: [+ {+ synthetic_field_060: "synthetic-input-344",+ },+ {+ synthetic_field_060: "synthetic-input-345",+ },+ ],+ properties: {+ synthetic_field_060: {+ description: "Synthetic description 346.",+ synthetic_field_021: [+ "synthetic-value-347",+ "synthetic-value-348",+ ],+ synthetic_field_036: 1,+ type: "string",+ },+ },+ required: ["synthetic_field_060"],+ type: "object",+ },+ },+ {+ type: "function",+ name: "synthetic_tool_007",+ description: "Synthetic description 349.",+ parameters: {+ additionalProperties: false,+ synthetic_field_021: [+ {+ synthetic_field_022: "synthetic_id_002",+ status: "synthetic-value-350",+ },+ ],+ properties: {+ synthetic_field_022: {+ description: "Synthetic description 351.",+ format: "synthetic-value-352",+ type: "string",+ },+ status: {+ description: "Synthetic description 353.",+ synthetic_field_021: ["synthetic-value-354"],+ anyOf: [+ {+ synthetic_field_038: "synthetic-value-355",+ },+ ],+ },+ },+ required: ["synthetic_field_022", "synthetic_field_061"],+ type: "object",+ synthetic_field_042: {+ synthetic_field_062: {+ enum: [+ "synthetic-value-356",+ "synthetic-value-357",+ "synthetic-value-358",+ ],+ type: "string",+ },+ },+ },+ },+ {+ type: "function",+ name: "synthetic_tool_008",+ description: "Synthetic description 359.",+ parameters: {+ additionalProperties: false,+ properties: {},+ type: "object",+ },+ },+ {+ type: "function",+ name: "synthetic_tool_009",+ description: "Synthetic description 360.",+ parameters: {+ additionalProperties: false,+ synthetic_field_021: [+ {+ synthetic_field_063: [+ {+ description: "Synthetic description 361.",+ synthetic_field_034: "synthetic-value-362",+ },+ {+ description: "Synthetic description 363.",+ synthetic_field_034: "synthetic-value-364",+ },+ ],+ synthetic_field_022: "synthetic_id_002",+ },+ ],+ properties: {+ synthetic_field_022: {+ description: "Synthetic description 365.",+ format: "synthetic-value-366",+ type: "string",+ },+ synthetic_field_063: {+ description: "Synthetic description 367.",+ items: {+ synthetic_field_038: "synthetic-value-368",+ },+ synthetic_field_064: 5,+ type: "synthetic-value-369",+ },+ },+ required: ["synthetic_field_022", "synthetic_field_063"],+ type: "object",+ synthetic_field_042: {+ synthetic_field_065: {+ additionalProperties: false,+ properties: {+ synthetic_field_034: {+ description: "Synthetic description 370.",+ synthetic_field_035: 255,+ type: "string",+ },+ description: {+ description: "Synthetic description 371.",+ type: "string",+ },+ },+ required: ["synthetic_field_034", "synthetic_field_066"],+ type: "object",+ },+ },+ },+ },+ {+ type: "function",+ name: "synthetic_tool_010",+ description: "Synthetic description 372.",+ parameters: {+ additionalProperties: false,+ synthetic_field_021: [+ {+ synthetic_field_022: "synthetic_id_002",+ synthetic_field_067: [+ {+ synthetic_field_068: "synthetic-value-373",+ required: true,+ type: "boolean",+ },+ {+ synthetic_field_068: "synthetic-value-374",+ required: false,+ type: "text",+ },+ ],+ },+ ],+ properties: {+ synthetic_field_022: {+ description: "Synthetic description 375.",+ format: "synthetic-value-376",+ type: "string",+ },+ synthetic_field_067: {+ description: "Synthetic description 377.",+ items: {+ synthetic_field_038: "synthetic-value-378",+ },+ type: "synthetic-value-379",+ },+ },+ required: ["synthetic_field_022", "synthetic_field_067"],+ type: "object",+ synthetic_field_042: {+ synthetic_field_069: {+ additionalProperties: false,+ properties: {+ synthetic_field_068: {+ description: "Synthetic description 380.",+ synthetic_field_036: 1,+ type: "string",+ },+ type: {+ description: "Synthetic description 381.",+ enum: ["text", "boolean", "synthetic-value-382"],+ type: "string",+ },+ required: {+ description: "Synthetic description 383.",+ type: "boolean",+ },+ },+ required: [+ "synthetic_field_068",+ "synthetic_field_070",+ "synthetic_field_071",+ ],+ type: "object",+ },+ },+ },+ },+ {+ type: "function",+ name: "synthetic_tool_011",+ description: "Synthetic description 384.",+ parameters: {+ additionalProperties: false,+ properties: {+ synthetic_field_072: {+ description: "Synthetic description 385.",+ synthetic_field_036: 1,+ type: "string",+ },+ },+ required: ["synthetic_field_072"],+ type: "object",+ },+ },+ ],+ },+ output:+ '[{"role":"assistant","parts":[{"type":"tool_call","id":"call_001","name":"synthetic_tool_011","arguments":"{\\"synthetic_field_072\\":\\"Synthetic assistant response 392.\\"}"}],"finish_reason":"tool_call"}]',+ metadata: {+ attributes: {+ "gen_ai.operation.name": "chat",+ "gen_ai.provider.name": "openai",+ "gen_ai.system": "synthetic-value-015",+ "synthetic.metadata.006": "synthetic-value-016",+ "gen_ai.request.model": "gpt-5.4-mini-2026-03-17",+ "gen_ai.agent.name": "synthetic-value-017",+ "gen_ai.agent.call.id": "synthetic-value-018",+ "gen_ai.conversation.id": "synthetic-value-019",+ "synthetic.metadata.007":+ '{"synthetic_field_006":[{"name":"synthetic_name_002","parameters_json_schema":{"additionalProperties":false,"properties":{"url":{"type":"string"}},"required":["synthetic_field_007"],"type":"object"},"description":"Synthetic description 020.","synthetic_field_008":null,"synthetic_field_009":true,"synthetic_field_010":false,"kind":"function","synthetic_field_011":null,"synthetic_field_012":null,"synthetic_field_013":false,"synthetic_field_014":null,"synthetic_field_015":null,"synthetic_field_016":null,"synthetic_field_017":null,"synthetic_field_018":null,"synthetic_field_019":"synthetic_id_001","synthetic_field_020":null},{"name":"synthetic_name_003","parameters_json_schema":{"additionalProperties":false,"properties":{},"type":"object"},"description":"Synthetic description 021.","synthetic_field_008":null,"synthetic_field_009":false,"synthetic_field_010":false,"kind":"function","synthetic_field_011":null,"synthetic_field_012":null,"synthetic_field_013":false,"synthetic_field_014":null,"synthetic_field_015":null,"synthetic_field_016":null,"synthetic_field_017":null,"synthetic_field_018":null,"synthetic_field_019":"synthetic_id_001","synthetic_field_020":null},{"name":"synthetic_name_004","parameters_json_schema":{"additionalProperties":false,"synthetic_field_021":[{"synthetic_field_022":"synthetic_id_002"}],"properties":{"synthetic_field_022":{"description":"Synthetic description 022.","format":"synthetic-value-023","type":"string"}},"required":["synthetic_field_022"],"type":"object"},"description":"Synthetic description 024.","synthetic_field_008":null,"synthetic_field_009":true,"synthetic_field_010":false,"kind":"function","synthetic_field_011":null,"synthetic_field_012":null,"synthetic_field_013":false,"synthetic_field_014":null,"synthetic_field_015":null,"synthetic_field_016":null,"synthetic_field_017":null,"synthetic_field_018":null,"synthetic_field_019":"synthetic_id_001","synthetic_field_020":null},{"name":"synthetic_name_005","parameters_json_schema":{"additionalProperties":false,"synthetic_field_021":[{"synthetic_field_023":"synthetic-value-025","synthetic_field_024":"synthetic-value-026","synthetic_field_025":"synthetic-value-027","synthetic_field_026":"synthetic-value-028","synthetic_field_027":"synthetic-value-029","synthetic_field_028":"synthetic-value-030","synthetic_field_029":"synthetic-value-031","synthetic_field_030":"synthetic-value-032","synthetic_field_031":85000,"synthetic_field_032":65000,"synthetic_field_033":"synthetic-value-033","synthetic_field_034":"synthetic-value-034"}],"properties":{"synthetic_field_034":{"description":"Synthetic description 035.","synthetic_field_021":["synthetic-value-036"],"synthetic_field_035":255,"synthetic_field_036":1,"type":"string"},"synthetic_field_029":{"synthetic_field_037":null,"description":"Synthetic description 037.","anyOf":[{"type":"string"},{"type":"null"}]},"synthetic_field_026":{"synthetic_field_037":null,"description":"Synthetic description 038.","anyOf":[{"type":"string"},{"type":"null"}]},"synthetic_field_027":{"synthetic_field_037":null,"description":"Synthetic description 039.","anyOf":[{"type":"string"},{"type":"null"}]},"synthetic_field_023":{"synthetic_field_037":null,"description":"Synthetic description 040.","anyOf":[{"type":"string"},{"type":"null"}]},"synthetic_field_024":{"synthetic_field_037":null,"description":"Synthetic description 041.","anyOf":[{"type":"string"},{"type":"null"}]},"synthetic_field_025":{"synthetic_field_037":null,"description":"Synthetic description 042.","synthetic_field_021":["synthetic-value-043"],"anyOf":[{"synthetic_field_038":"synthetic-value-044"},{"type":"null"}]},"synthetic_field_028":{"synthetic_field_037":null,"description":"Synthetic description 045.","synthetic_field_021":["synthetic-value-046"],"anyOf":[{"synthetic_field_038":"synthetic-value-047"},{"type":"null"}]},"synthetic_field_033":{"synthetic_field_037":null,"description":"Synthetic description 048.","anyOf":[{"type":"string"},{"type":"null"}]},"synthetic_field_039":{"synthetic_field_037":null,"description":"Synthetic description 049.","anyOf":[{"items":{"synthetic_field_038":"synthetic-value-050"},"type":"synthetic-value-051"},{"type":"null"}]},"synthetic_field_030":{"synthetic_field_037":null,"description":"Synthetic description 052.","synthetic_field_021":["synthetic-value-053"],"anyOf":[{"synthetic_field_038":"synthetic-value-054"},{"type":"null"}]},"synthetic_field_032":{"synthetic_field_037":null,"description":"Synthetic description 055.","anyOf":[{"type":"integer"},{"type":"null"}]},"synthetic_field_031":{"synthetic_field_037":null,"description":"Synthetic description 056.","anyOf":[{"type":"integer"},{"type":"null"}]},"synthetic_field_040":{"synthetic_field_037":null,"description":"Synthetic description 057.","anyOf":[{"type":"integer"},{"type":"null"}]},"synthetic_field_041":{"synthetic_field_037":null,"description":"Synthetic description 058.","anyOf":[{"type":"integer"},{"type":"null"}]}},"required":["synthetic_field_034"],"type":"object","synthetic_field_042":{"synthetic_field_043":{"enum":["synthetic-value-059","synthetic-value-060","synthetic-value-061","synthetic-value-062","synthetic-value-063","synthetic-value-064","synthetic-value-065","synthetic-value-066","synthetic-value-067","synthetic-value-068"],"type":"string"},"synthetic_field_044":{"enum":["synthetic-value-069","synthetic-value-070","synthetic-value-071"],"type":"string"},"synthetic_field_045":{"enum":["synthetic-value-072","synthetic-value-073","synthetic-value-074"],"type":"string"},"synthetic_field_046":{"properties":{"synthetic_field_047":{"description":"Synthetic description 075.","type":"string"},"synthetic_field_048":{"synthetic_field_037":null,"description":"Synthetic description 076.","synthetic_field_021":["synthetic-value-077"],"anyOf":[{"type":"string"},{"type":"null"}]},"synthetic_field_049":{"synthetic_field_037":null,"description":"Synthetic description 078.","synthetic_field_021":["synthetic-value-079"],"anyOf":[{"type":"string"},{"type":"null"}]},"synthetic_field_050":{"synthetic_field_037":null,"description":"Synthetic description 080.","anyOf":[{"type":"string"},{"type":"null"}]},"synthetic_field_051":{"synthetic_field_037":null,"description":"Synthetic description 081.","anyOf":[{"type":"string"},{"type":"null"}]},"synthetic_field_052":{"synthetic_field_037":null,"description":"Synthetic description 082.","anyOf":[{"type":"string"},{"type":"null"}]},"synthetic_field_053":{"synthetic_field_037":null,"description":"Synthetic description 083.","anyOf":[{"type":"string"},{"type":"null"}]},"synthetic_field_054":{"description":"Synthetic description 084.","synthetic_field_021":["synthetic-value-085"],"type":"string"},"synthetic_field_055":{"synthetic_field_037":null,"description":"Synthetic description 086.","anyOf":[{"type":"string"},{"type":"null"}]},"synthetic_field_056":{"description":"Synthetic description 087.","type":"string"},"synthetic_field_057":{"synthetic_field_037":null,"description":"Synthetic description 088.","anyOf":[{"type":"string"},{"type":"null"}]},"synthetic_field_058":{"synthetic_field_037":null,"description":"Synthetic description 089.","anyOf":[{"type":"string"},{"type":"null"}]},"synthetic_field_059":{"synthetic_field_037":null,"description":"Synthetic description 090.","anyOf":[{"type":"string"},{"type":"null"}]}},"required":["synthetic_field_047","synthetic_field_054","synthetic_field_056"],"type":"object","additionalProperties":false}}},"description":"Synthetic description 091.","synthetic_field_008":null,"synthetic_field_009":false,"synthetic_field_010":false,"kind":"function","synthetic_field_011":null,"synthetic_field_012":null,"synthetic_field_013":false,"synthetic_field_014":null,"synthetic_field_015":null,"synthetic_field_016":null,"synthetic_field_017":null,"synthetic_field_018":null,"synthetic_field_019":"synthetic_id_001","synthetic_field_020":null},{"name":"synthetic_name_006","parameters_json_schema":{"additionalProperties":false,"synthetic_field_021":[{"synthetic_field_022":"synthetic_id_002","synthetic_field_028":"synthetic-value-092","synthetic_field_031":90000,"synthetic_field_032":70000,"synthetic_field_033":"synthetic-value-093"}],"properties":{"synthetic_field_022":{"description":"Synthetic description 094.","format":"synthetic-value-095","type":"string"},"synthetic_field_034":{"synthetic_field_037":null,"description":"Synthetic description 096.","synthetic_field_021":["synthetic-value-097"],"anyOf":[{"synthetic_field_035":255,"synthetic_field_036":1,"type":"string"},{"type":"null"}]},"synthetic_field_029":{"synthetic_field_037":null,"description":"Synthetic description 098.","anyOf":[{"type":"string"},{"type":"null"}]},"synthetic_field_026":{"synthetic_field_037":null,"description":"Synthetic description 099.","anyOf":[{"type":"string"},{"type":"null"}]},"synthetic_field_027":{"synthetic_field_037":null,"description":"Synthetic description 100.","anyOf":[{"type":"string"},{"type":"null"}]},"synthetic_field_023":{"synthetic_field_037":null,"description":"Synthetic description 101.","anyOf":[{"type":"string"},{"type":"null"}]},"synthetic_field_024":{"synthetic_field_037":null,"description":"Synthetic description 102.","anyOf":[{"type":"string"},{"type":"null"}]},"synthetic_field_025":{"synthetic_field_037":null,"description":"Synthetic description 103.","synthetic_field_021":["synthetic-value-104"],"anyOf":[{"synthetic_field_038":"synthetic-value-105"},{"type":"null"}]},"synthetic_field_028":{"synthetic_field_037":null,"description":"Synthetic description 106.","synthetic_field_021":["synthetic-value-107"],"anyOf":[{"synthetic_field_038":"synthetic-value-108"},{"type":"null"}]},"synthetic_field_033":{"synthetic_field_037":null,"description":"Synthetic description 109.","anyOf":[{"type":"string"},{"type":"null"}]},"synthetic_field_039":{"synthetic_field_037":null,"description":"Synthetic description 110.","anyOf":[{"items":{"synthetic_field_038":"synthetic-value-111"},"type":"synthetic-value-112"},{"type":"null"}]},"synthetic_field_030":{"synthetic_field_037":null,"description":"Synthetic description 113.","synthetic_field_021":["synthetic-value-114"],"anyOf":[{"synthetic_field_038":"synthetic-value-115"},{"type":"null"}]},"synthetic_field_032":{"synthetic_field_037":null,"description":"Synthetic description 116.","anyOf":[{"type":"integer"},{"type":"null"}]},"synthetic_field_031":{"synthetic_field_037":null,"description":"Synthetic description 117.","anyOf":[{"type":"integer"},{"type":"null"}]},"synthetic_field_040":{"synthetic_field_037":null,"description":"Synthetic description 118.","anyOf":[{"type":"integer"},{"type":"null"}]},"synthetic_field_041":{"synthetic_field_037":null,"description":"Synthetic description 119.","anyOf":[{"type":"integer"},{"type":"null"}]}},"required":["synthetic_field_022"],"type":"object","synthetic_field_042":{"synthetic_field_043":{"enum":["synthetic-value-120","synthetic-value-121","synthetic-value-122","synthetic-value-123","synthetic-value-124","synthetic-value-125","synthetic-value-126","synthetic-value-127","synthetic-value-128","synthetic-value-129"],"type":"string"},"synthetic_field_044":{"enum":["synthetic-value-130","synthetic-value-131","synthetic-value-132"],"type":"string"},"synthetic_field_045":{"enum":["synthetic-value-133","synthetic-value-134","synthetic-value-135"],"type":"string"},"synthetic_field_046":{"properties":{"synthetic_field_047":{"description":"Synthetic description 136.","type":"string"},"synthetic_field_048":{"synthetic_field_037":null,"description":"Synthetic description 137.","synthetic_field_021":["synthetic-value-138"],"anyOf":[{"type":"string"},{"type":"null"}]},"synthetic_field_049":{"synthetic_field_037":null,"description":"Synthetic description 139.","synthetic_field_021":["synthetic-value-140"],"anyOf":[{"type":"string"},{"type":"null"}]},"synthetic_field_050":{"synthetic_field_037":null,"description":"Synthetic description 141.","anyOf":[{"type":"string"},{"type":"null"}]},"synthetic_field_051":{"synthetic_field_037":null,"description":"Synthetic description 142.","anyOf":[{"type":"string"},{"type":"null"}]},"synthetic_field_052":{"synthetic_field_037":null,"description":"Synthetic description 143.","anyOf":[{"type":"string"},{"type":"null"}]},"synthetic_field_053":{"synthetic_field_037":null,"description":"Synthetic description 144.","anyOf":[{"type":"string"},{"type":"null"}]},"synthetic_field_054":{"description":"Synthetic description 145.","synthetic_field_021":["synthetic-value-146"],"type":"string"},"synthetic_field_055":{"synthetic_field_037":null,"description":"Synthetic description 147.","anyOf":[{"type":"string"},{"type":"null"}]},"synthetic_field_056":{"description":"Synthetic description 148.","type":"string"},"synthetic_field_057":{"synthetic_field_037":null,"description":"Synthetic description 149.","anyOf":[{"type":"string"},{"type":"null"}]},"synthetic_field_058":{"synthetic_field_037":null,"description":"Synthetic description 150.","anyOf":[{"type":"string"},{"type":"null"}]},"synthetic_field_059":{"synthetic_field_037":null,"description":"Synthetic description 151.","anyOf":[{"type":"string"},{"type":"null"}]}},"required":["synthetic_field_047","synthetic_field_054","synthetic_field_056"],"type":"object","additionalProperties":false}}},"description":"Synthetic description 152.","synthetic_field_008":null,"synthetic_field_009":false,"synthetic_field_010":false,"kind":"function","synthetic_field_011":null,"synthetic_field_012":null,"synthetic_field_013":false,"synthetic_field_014":null,"synthetic_field_015":null,"synthetic_field_016":null,"synthetic_field_017":null,"synthetic_field_018":null,"synthetic_field_019":"synthetic_id_001","synthetic_field_020":null},{"name":"synthetic_name_007","parameters_json_schema":{"additionalProperties":false,"synthetic_field_021":[{"synthetic_field_060":"synthetic-input-153"},{"synthetic_field_060":"synthetic-input-154"}],"properties":{"synthetic_field_060":{"description":"Synthetic description 155.","synthetic_field_021":["synthetic-value-156","synthetic-value-157"],"synthetic_field_036":1,"type":"string"}},"required":["synthetic_field_060"],"type":"object"},"description":"Synthetic description 158.","synthetic_field_008":null,"synthetic_field_009":false,"synthetic_field_010":false,"kind":"function","synthetic_field_011":null,"synthetic_field_012":null,"synthetic_field_013":false,"synthetic_field_014":null,"synthetic_field_015":null,"synthetic_field_016":null,"synthetic_field_017":null,"synthetic_field_018":null,"synthetic_field_019":"synthetic_id_001","synthetic_field_020":null},{"name":"synthetic_name_008","parameters_json_schema":{"additionalProperties":false,"synthetic_field_021":[{"synthetic_field_022":"synthetic_id_002","status":"synthetic-value-159"}],"properties":{"synthetic_field_022":{"description":"Synthetic description 160.","format":"synthetic-value-161","type":"string"},"status":{"description":"Synthetic description 162.","synthetic_field_021":["synthetic-value-163"],"anyOf":[{"synthetic_field_038":"synthetic-value-164"}]}},"required":["synthetic_field_022","synthetic_field_061"],"type":"object","synthetic_field_042":{"synthetic_field_062":{"enum":["synthetic-value-165","synthetic-value-166","synthetic-value-167"],"type":"string"}}},"description":"Synthetic description 168.","synthetic_field_008":null,"synthetic_field_009":true,"synthetic_field_010":false,"kind":"function","synthetic_field_011":null,"synthetic_field_012":null,"synthetic_field_013":false,"synthetic_field_014":null,"synthetic_field_015":null,"synthetic_field_016":null,"synthetic_field_017":null,"synthetic_field_018":null,"synthetic_field_019":"synthetic_id_001","synthetic_field_020":null},{"name":"synthetic_name_009","parameters_json_schema":{"additionalProperties":false,"properties":{},"type":"object"},"description":"Synthetic description 169.","synthetic_field_008":null,"synthetic_field_009":false,"synthetic_field_010":false,"kind":"function","synthetic_field_011":null,"synthetic_field_012":null,"synthetic_field_013":false,"synthetic_field_014":null,"synthetic_field_015":null,"synthetic_field_016":null,"synthetic_field_017":null,"synthetic_field_018":null,"synthetic_field_019":"synthetic_id_001","synthetic_field_020":null},{"name":"synthetic_name_010","parameters_json_schema":{"additionalProperties":false,"synthetic_field_021":[{"synthetic_field_063":[{"description":"Synthetic description 170.","synthetic_field_034":"synthetic-value-171"},{"description":"Synthetic description 172.","synthetic_field_034":"synthetic-value-173"}],"synthetic_field_022":"synthetic_id_002"}],"properties":{"synthetic_field_022":{"description":"Synthetic description 174.","format":"synthetic-value-175","type":"string"},"synthetic_field_063":{"description":"Synthetic description 176.","items":{"synthetic_field_038":"synthetic-value-177"},"synthetic_field_064":5,"type":"synthetic-value-178"}},"required":["synthetic_field_022","synthetic_field_063"],"type":"object","synthetic_field_042":{"synthetic_field_065":{"additionalProperties":false,"properties":{"synthetic_field_034":{"description":"Synthetic description 179.","synthetic_field_035":255,"type":"string"},"description":{"description":"Synthetic description 180.","type":"string"}},"required":["synthetic_field_034","synthetic_field_066"],"type":"object"}}},"description":"Synthetic description 181.","synthetic_field_008":null,"synthetic_field_009":false,"synthetic_field_010":false,"kind":"function","synthetic_field_011":null,"synthetic_field_012":null,"synthetic_field_013":false,"synthetic_field_014":null,"synthetic_field_015":null,"synthetic_field_016":null,"synthetic_field_017":null,"synthetic_field_018":null,"synthetic_field_019":"synthetic_id_001","synthetic_field_020":null},{"name":"synthetic_name_011","parameters_json_schema":{"additionalProperties":false,"synthetic_field_021":[{"synthetic_field_022":"synthetic_id_002","synthetic_field_067":[{"synthetic_field_068":"synthetic-value-182","required":true,"type":"boolean"},{"synthetic_field_068":"synthetic-value-183","required":false,"type":"text"}]}],"properties":{"synthetic_field_022":{"description":"Synthetic description 184.","format":"synthetic-value-185","type":"string"},"synthetic_field_067":{"description":"Synthetic description 186.","items":{"synthetic_field_038":"synthetic-value-187"},"type":"synthetic-value-188"}},"required":["synthetic_field_022","synthetic_field_067"],"type":"object","synthetic_field_042":{"synthetic_field_069":{"additionalProperties":false,"properties":{"synthetic_field_068":{"description":"Synthetic description 189.","synthetic_field_036":1,"type":"string"},"type":{"description":"Synthetic description 190.","enum":["text","boolean","synthetic-value-191"],"type":"string"},"required":{"description":"Synthetic description 192.","type":"boolean"}},"required":["synthetic_field_068","synthetic_field_070","synthetic_field_071"],"type":"object"}}},"description":"Synthetic description 193.","synthetic_field_008":null,"synthetic_field_009":false,"synthetic_field_010":false,"kind":"function","synthetic_field_011":null,"synthetic_field_012":null,"synthetic_field_013":false,"synthetic_field_014":null,"synthetic_field_015":null,"synthetic_field_016":null,"synthetic_field_017":null,"synthetic_field_018":null,"synthetic_field_019":"synthetic_id_001","synthetic_field_020":null},{"name":"synthetic_name_012","parameters_json_schema":{"additionalProperties":false,"properties":{"synthetic_field_072":{"description":"Synthetic description 194.","synthetic_field_036":1,"type":"string"}},"required":["synthetic_field_072"],"type":"object"},"description":"Synthetic description 195.","synthetic_field_008":null,"synthetic_field_009":false,"synthetic_field_010":false,"kind":"function","synthetic_field_011":null,"synthetic_field_012":null,"synthetic_field_013":false,"synthetic_field_014":null,"synthetic_field_015":null,"synthetic_field_016":null,"synthetic_field_017":null,"synthetic_field_018":null,"synthetic_field_019":"synthetic_id_001","synthetic_field_020":null}],"synthetic_field_073":[],"synthetic_field_074":{"synthetic_field_075":"synthetic-value-196","synthetic_field_076":"synthetic-value-197","synthetic_field_077":"synthetic-value-198","synthetic_field_078":"synthetic-value-199","synthetic_field_079":"synthetic-value-200","synthetic_field_080":"synthetic-value-201","synthetic_field_081":"synthetic-value-202","synthetic_field_082":"synthetic-value-203","synthetic_field_083":"synthetic-value-204","synthetic_field_084":"synthetic-value-205","synthetic_field_085":"synthetic-value-206"},"synthetic_field_086":[],"synthetic_field_087":"synthetic-output-207","synthetic_field_088":null,"synthetic_field_089":[],"synthetic_field_090":null,"synthetic_field_091":true,"synthetic_field_092":false,"synthetic_field_093":[{"content":"synthetic-value-208","synthetic_field_094":false,"synthetic_field_095":"synthetic-value-209"}],"thinking":null}',+ "gen_ai.request.max_tokens": '{"intValue":"synthetic-value-386"}',+ "synthetic.metadata.008": "synthetic-value-387",+ "synthetic.metadata.009": "synthetic-value-388",+ "langfuse.trace.name": "synthetic-value-389",+ "langfuse.environment": "synthetic-value-390",+ "synthetic.metadata.010":+ '{"type":"object","properties":{"gen_ai.input.messages":{"type":"synthetic-value-394"},"gen_ai.output.messages":{"type":"synthetic-value-395"},"gen_ai.system_instructions":{"type":"synthetic-value-396"},"synthetic_field_096":{"type":"object"}}}',+ "gen_ai.usage.input_tokens": '{"intValue":"synthetic-value-397"}',+ "gen_ai.usage.output_tokens": '{"intValue":"synthetic-value-398"}',+ "gen_ai.usage.details.accepted_prediction_tokens":+ '{"intValue":"synthetic-value-399"}',+ "gen_ai.usage.details.audio_tokens":+ '{"intValue":"synthetic-value-400"}',+ "gen_ai.usage.details.reasoning_tokens":+ '{"intValue":"synthetic-value-401"}',+ "gen_ai.usage.details.rejected_prediction_tokens":+ '{"intValue":"synthetic-value-402"}',+ "gen_ai.response.model": "synthetic-value-403",+ "synthetic.metadata.011": "0.005547",+ "gen_ai.response.id": "synthetic-value-404",+ "gen_ai.response.finish_reasons": '["synthetic-value-405"]',+ "gen_ai.client.operation.time_to_first_chunk": "1.18922124899996",+ },+ resourceAttributes: {+ "telemetry.sdk.language": "python",+ "telemetry.sdk.name": "opentelemetry",+ "telemetry.sdk.version": "1.39.1",+ "synthetic.metadata.001": "synthetic-value-008",+ "service.version": "synthetic-value-009",+ "synthetic.metadata.002": "synthetic-value-010",+ "synthetic.metadata.003": "synthetic-value-011",+ "synthetic.metadata.004": "synthetic-value-012",+ "synthetic.metadata.005": "synthetic-value-013",+ "service.name": "synthetic-service",+ "telemetry.auto.version": "synthetic-value-014",+ },+ scope: {+ name: "pydantic-ai",+ version: "2.29.0",+ attributes: {},+ },+ },+ },+ expected: {+ messages: [+ {+ role: "system",+ parts: [+ {+ type: "text",+ text: "synthetic-value-393",+ },+ ],+ source: "input",+ },+ {+ role: "user",+ parts: [+ {+ type: "text",+ text: "Synthetic user request 391.",+ },+ ],+ source: "input",+ },+ {+ role: "assistant",+ parts: [+ {+ type: "tool-call",+ toolCallId: "call_001",+ toolName: "synthetic_tool_011",+ input: {+ synthetic_field_072: "Synthetic assistant response 392.",+ },+ toolType: "tool_call",+ },+ ],+ finishReason: { type: "tool-calls", raw: "tool_call" },+ source: "output",+ },+ ],+ toolDefinitions: [+ {+ name: "synthetic_tool_001",+ description: "Synthetic description 210.",+ inputSchema: {+ additionalProperties: false,+ properties: {+ url: {+ type: "string",+ },+ },+ required: ["synthetic_field_007"],+ type: "object",+ },+ type: "function",+ },+ {+ name: "synthetic_tool_002",+ description: "Synthetic description 211.",+ inputSchema: {+ additionalProperties: false,+ properties: {},+ type: "object",+ },+ type: "function",+ },+ {+ name: "synthetic_tool_003",+ description: "Synthetic description 212.",+ inputSchema: {+ additionalProperties: false,+ synthetic_field_021: [+ {+ synthetic_field_022: "synthetic_id_002",+ },+ ],+ properties: {+ synthetic_field_022: {+ description: "Synthetic description 213.",+ format: "synthetic-value-214",+ type: "string",+ },+ },+ required: ["synthetic_field_022"],+ type: "object",+ },+ type: "function",+ },+ {+ name: "synthetic_tool_004",+ description: "Synthetic description 215.",+ inputSchema: {+ additionalProperties: false,+ synthetic_field_021: [+ {+ synthetic_field_023: "synthetic-value-216",+ synthetic_field_024: "synthetic-value-217",+ synthetic_field_025: "synthetic-value-218",+ synthetic_field_026: "synthetic-value-219",+ synthetic_field_027: "synthetic-value-220",+ synthetic_field_028: "synthetic-value-221",+ synthetic_field_029: "synthetic-value-222",+ synthetic_field_030: "synthetic-value-223",+ synthetic_field_031: 85000,+ synthetic_field_032: 65000,+ synthetic_field_033: "synthetic-value-224",+ synthetic_field_034: "synthetic-value-225",+ },+ ],+ properties: {+ synthetic_field_034: {+ description: "Synthetic description 226.",+ synthetic_field_021: ["synthetic-value-227"],+ synthetic_field_035: 255,+ synthetic_field_036: 1,+ type: "string",+ },+ synthetic_field_029: {+ synthetic_field_037: null,+ description: "Synthetic description 228.",+ anyOf: [+ {+ type: "string",+ },+ {+ type: "null",+ },+ ],+ },+ synthetic_field_026: {+ synthetic_field_037: null,+ description: "Synthetic description 229.",+ anyOf: [+ {+ type: "string",+ },+ {+ type: "null",+ },+ ],+ },+ synthetic_field_027: {+ synthetic_field_037: null,+ description: "Synthetic description 230.",+ anyOf: [+ {+ type: "string",+ },+ {+ type: "null",+ },+ ],+ },+ synthetic_field_023: {+ synthetic_field_037: null,+ description: "Synthetic description 231.",+ anyOf: [+ {+ type: "string",+ },+ {+ type: "null",+ },+ ],+ },+ synthetic_field_024: {+ synthetic_field_037: null,+ description: "Synthetic description 232.",+ anyOf: [+ {+ type: "string",+ },+ {+ type: "null",+ },+ ],+ },+ synthetic_field_025: {+ synthetic_field_037: null,+ description: "Synthetic description 233.",+ synthetic_field_021: ["synthetic-value-234"],+ anyOf: [+ {+ synthetic_field_038: "synthetic-value-235",+ },+ {+ type: "null",+ },+ ],+ },+ synthetic_field_028: {+ synthetic_field_037: null,+ description: "Synthetic description 236.",+ synthetic_field_021: ["synthetic-value-237"],+ anyOf: [+ {+ synthetic_field_038: "synthetic-value-238",+ },+ {+ type: "null",+ },+ ],+ },+ synthetic_field_033: {+ synthetic_field_037: null,+ description: "Synthetic description 239.",+ anyOf: [+ {+ type: "string",+ },+ {+ type: "null",+ },+ ],+ },+ synthetic_field_039: {+ synthetic_field_037: null,+ description: "Synthetic description 240.",+ anyOf: [+ {+ items: {+ synthetic_field_038: "synthetic-value-241",+ },+ type: "synthetic-value-242",+ },+ {+ type: "null",+ },+ ],+ },+ synthetic_field_030: {+ synthetic_field_037: null,+ description: "Synthetic description 243.",+ synthetic_field_021: ["synthetic-value-244"],+ anyOf: [+ {+ synthetic_field_038: "synthetic-value-245",+ },+ {+ type: "null",+ },+ ],+ },+ synthetic_field_032: {+ synthetic_field_037: null,+ description: "Synthetic description 246.",+ anyOf: [+ {+ type: "integer",+ },+ {+ type: "null",+ },+ ],+ },+ synthetic_field_031: {+ synthetic_field_037: null,+ description: "Synthetic description 247.",+ anyOf: [+ {+ type: "integer",+ },+ {+ type: "null",+ },+ ],+ },+ synthetic_field_040: {+ synthetic_field_037: null,+ description: "Synthetic description 248.",+ anyOf: [+ {+ type: "integer",+ },+ {+ type: "null",+ },+ ],+ },+ synthetic_field_041: {+ synthetic_field_037: null,+ description: "Synthetic description 249.",+ anyOf: [+ {+ type: "integer",+ },+ {+ type: "null",+ },+ ],+ },+ },+ required: ["synthetic_field_034"],+ type: "object",+ synthetic_field_042: {+ synthetic_field_043: {+ enum: [+ "synthetic-value-250",+ "synthetic-value-251",+ "synthetic-value-252",+ "synthetic-value-253",+ "synthetic-value-254",+ "synthetic-value-255",+ "synthetic-value-256",+ "synthetic-value-257",+ "synthetic-value-258",+ "synthetic-value-259",+ ],+ type: "string",+ },+ synthetic_field_044: {+ enum: [+ "synthetic-value-260",+ "synthetic-value-261",+ "synthetic-value-262",+ ],+ type: "string",+ },+ synthetic_field_045: {+ enum: [+ "synthetic-value-263",+ "synthetic-value-264",+ "synthetic-value-265",+ ],+ type: "string",+ },+ synthetic_field_046: {+ properties: {+ synthetic_field_047: {+ description: "Synthetic description 266.",+ type: "string",+ },+ synthetic_field_048: {+ synthetic_field_037: null,+ description: "Synthetic description 267.",+ synthetic_field_021: ["synthetic-value-268"],+ anyOf: [+ {+ type: "string",+ },+ {+ type: "null",+ },+ ],+ },+ synthetic_field_049: {+ synthetic_field_037: null,+ description: "Synthetic description 269.",+ synthetic_field_021: ["synthetic-value-270"],+ anyOf: [+ {+ type: "string",+ },+ {+ type: "null",+ },+ ],+ },+ synthetic_field_050: {+ synthetic_field_037: null,+ description: "Synthetic description 271.",+ anyOf: [+ {+ type: "string",+ },+ {+ type: "null",+ },+ ],+ },+ synthetic_field_051: {+ synthetic_field_037: null,+ description: "Synthetic description 272.",+ anyOf: [+ {+ type: "string",+ },+ {+ type: "null",+ },+ ],+ },+ synthetic_field_052: {+ synthetic_field_037: null,+ description: "Synthetic description 273.",+ anyOf: [+ {+ type: "string",+ },+ {+ type: "null",+ },+ ],+ },+ synthetic_field_053: {+ synthetic_field_037: null,+ description: "Synthetic description 274.",+ anyOf: [+ {+ type: "string",+ },+ {+ type: "null",+ },+ ],+ },+ synthetic_field_054: {+ description: "Synthetic description 275.",+ synthetic_field_021: ["synthetic-value-276"],+ type: "string",+ },+ synthetic_field_055: {+ synthetic_field_037: null,+ description: "Synthetic description 277.",+ anyOf: [+ {+ type: "string",+ },+ {+ type: "null",+ },+ ],+ },+ synthetic_field_056: {+ description: "Synthetic description 278.",+ type: "string",+ },+ synthetic_field_057: {+ synthetic_field_037: null,+ description: "Synthetic description 279.",+ anyOf: [+ {+ type: "string",+ },+ {+ type: "null",+ },+ ],+ },+ synthetic_field_058: {+ synthetic_field_037: null,+ description: "Synthetic description 280.",+ anyOf: [+ {+ type: "string",+ },+ {+ type: "null",+ },+ ],+ },+ synthetic_field_059: {+ synthetic_field_037: null,+ description: "Synthetic description 281.",+ anyOf: [+ {+ type: "string",+ },+ {+ type: "null",+ },+ ],+ },+ },+ required: [+ "synthetic_field_047",+ "synthetic_field_054",+ "synthetic_field_056",+ ],+ type: "object",+ additionalProperties: false,+ },+ },+ },+ type: "function",+ },+ {+ name: "synthetic_tool_005",+ description: "Synthetic description 282.",+ inputSchema: {+ additionalProperties: false,+ synthetic_field_021: [+ {+ synthetic_field_022: "synthetic_id_002",+ synthetic_field_028: "synthetic-value-283",+ synthetic_field_031: 90000,+ synthetic_field_032: 70000,+ synthetic_field_033: "synthetic-value-284",+ },+ ],+ properties: {+ synthetic_field_022: {+ description: "Synthetic description 285.",+ format: "synthetic-value-286",+ type: "string",+ },+ synthetic_field_034: {+ synthetic_field_037: null,+ description: "Synthetic description 287.",+ synthetic_field_021: ["synthetic-value-288"],+ anyOf: [+ {+ synthetic_field_035: 255,+ synthetic_field_036: 1,+ type: "string",+ },+ {+ type: "null",+ },+ ],+ },+ synthetic_field_029: {+ synthetic_field_037: null,+ description: "Synthetic description 289.",+ anyOf: [+ {+ type: "string",+ },+ {+ type: "null",+ },+ ],+ },+ synthetic_field_026: {+ synthetic_field_037: null,+ description: "Synthetic description 290.",+ anyOf: [+ {+ type: "string",+ },+ {+ type: "null",+ },+ ],+ },+ synthetic_field_027: {+ synthetic_field_037: null,+ description: "Synthetic description 291.",+ anyOf: [+ {+ type: "string",+ },+ {+ type: "null",+ },+ ],+ },+ synthetic_field_023: {+ synthetic_field_037: null,+ description: "Synthetic description 292.",+ anyOf: [+ {+ type: "string",+ },+ {+ type: "null",+ },+ ],+ },+ synthetic_field_024: {+ synthetic_field_037: null,+ description: "Synthetic description 293.",+ anyOf: [+ {+ type: "string",+ },+ {+ type: "null",+ },+ ],+ },+ synthetic_field_025: {+ synthetic_field_037: null,+ description: "Synthetic description 294.",+ synthetic_field_021: ["synthetic-value-295"],+ anyOf: [+ {+ synthetic_field_038: "synthetic-value-296",+ },+ {+ type: "null",+ },+ ],+ },+ synthetic_field_028: {+ synthetic_field_037: null,+ description: "Synthetic description 297.",+ synthetic_field_021: ["synthetic-value-298"],+ anyOf: [+ {+ synthetic_field_038: "synthetic-value-299",+ },+ {+ type: "null",+ },+ ],+ },+ synthetic_field_033: {+ synthetic_field_037: null,+ description: "Synthetic description 300.",+ anyOf: [+ {+ type: "string",+ },+ {+ type: "null",+ },+ ],+ },+ synthetic_field_039: {+ synthetic_field_037: null,+ description: "Synthetic description 301.",+ anyOf: [+ {+ items: {+ synthetic_field_038: "synthetic-value-302",+ },+ type: "synthetic-value-303",+ },+ {+ type: "null",+ },+ ],+ },+ synthetic_field_030: {+ synthetic_field_037: null,+ description: "Synthetic description 304.",+ synthetic_field_021: ["synthetic-value-305"],+ anyOf: [+ {+ synthetic_field_038: "synthetic-value-306",+ },+ {+ type: "null",+ },+ ],+ },+ synthetic_field_032: {+ synthetic_field_037: null,+ description: "Synthetic description 307.",+ anyOf: [+ {+ type: "integer",+ },+ {+ type: "null",+ },+ ],+ },+ synthetic_field_031: {+ synthetic_field_037: null,+ description: "Synthetic description 308.",+ anyOf: [+ {+ type: "integer",+ },+ {+ type: "null",+ },+ ],+ },+ synthetic_field_040: {+ synthetic_field_037: null,+ description: "Synthetic description 309.",+ anyOf: [+ {+ type: "integer",+ },+ {+ type: "null",+ },+ ],+ },+ synthetic_field_041: {+ synthetic_field_037: null,+ description: "Synthetic description 310.",+ anyOf: [+ {+ type: "integer",+ },+ {+ type: "null",+ },+ ],+ },+ },+ required: ["synthetic_field_022"],+ type: "object",+ synthetic_field_042: {+ synthetic_field_043: {+ enum: [+ "synthetic-value-311",+ "synthetic-value-312",+ "synthetic-value-313",+ "synthetic-value-314",+ "synthetic-value-315",+ "synthetic-value-316",+ "synthetic-value-317",+ "synthetic-value-318",+ "synthetic-value-319",+ "synthetic-value-320",+ ],+ type: "string",+ },+ synthetic_field_044: {+ enum: [+ "synthetic-value-321",+ "synthetic-value-322",+ "synthetic-value-323",+ ],+ type: "string",+ },+ synthetic_field_045: {+ enum: [+ "synthetic-value-324",+ "synthetic-value-325",+ "synthetic-value-326",+ ],+ type: "string",+ },+ synthetic_field_046: {+ properties: {+ synthetic_field_047: {+ description: "Synthetic description 327.",+ type: "string",+ },+ synthetic_field_048: {+ synthetic_field_037: null,+ description: "Synthetic description 328.",+ synthetic_field_021: ["synthetic-value-329"],+ anyOf: [+ {+ type: "string",+ },+ {+ type: "null",+ },+ ],+ },+ synthetic_field_049: {+ synthetic_field_037: null,+ description: "Synthetic description 330.",+ synthetic_field_021: ["synthetic-value-331"],+ anyOf: [+ {+ type: "string",+ },+ {+ type: "null",+ },+ ],+ },+ synthetic_field_050: {+ synthetic_field_037: null,+ description: "Synthetic description 332.",+ anyOf: [+ {+ type: "string",+ },+ {+ type: "null",+ },+ ],+ },+ synthetic_field_051: {+ synthetic_field_037: null,+ description: "Synthetic description 333.",+ anyOf: [+ {+ type: "string",+ },+ {+ type: "null",+ },+ ],+ },+ synthetic_field_052: {+ synthetic_field_037: null,+ description: "Synthetic description 334.",+ anyOf: [+ {+ type: "string",+ },+ {+ type: "null",+ },+ ],+ },+ synthetic_field_053: {+ synthetic_field_037: null,+ description: "Synthetic description 335.",+ anyOf: [+ {+ type: "string",+ },+ {+ type: "null",+ },+ ],+ },+ synthetic_field_054: {+ description: "Synthetic description 336.",+ synthetic_field_021: ["synthetic-value-337"],+ type: "string",+ },+ synthetic_field_055: {+ synthetic_field_037: null,+ description: "Synthetic description 338.",+ anyOf: [+ {+ type: "string",+ },+ {+ type: "null",+ },+ ],+ },+ synthetic_field_056: {+ description: "Synthetic description 339.",+ type: "string",+ },+ synthetic_field_057: {+ synthetic_field_037: null,+ description: "Synthetic description 340.",+ anyOf: [+ {+ type: "string",+ },+ {+ type: "null",+ },+ ],+ },+ synthetic_field_058: {+ synthetic_field_037: null,+ description: "Synthetic description 341.",+ anyOf: [+ {+ type: "string",+ },+ {+ type: "null",+ },+ ],+ },+ synthetic_field_059: {+ synthetic_field_037: null,+ description: "Synthetic description 342.",+ anyOf: [+ {+ type: "string",+ },+ {+ type: "null",+ },+ ],+ },+ },+ required: [+ "synthetic_field_047",+ "synthetic_field_054",+ "synthetic_field_056",+ ],+ type: "object",+ additionalProperties: false,+ },+ },+ },+ type: "function",+ },+ {+ name: "synthetic_tool_006",+ description: "Synthetic description 343.",+ inputSchema: {+ additionalProperties: false,+ synthetic_field_021: [+ {+ synthetic_field_060: "synthetic-input-344",+ },+ {+ synthetic_field_060: "synthetic-input-345",+ },+ ],+ properties: {+ synthetic_field_060: {+ description: "Synthetic description 346.",+ synthetic_field_021: [+ "synthetic-value-347",+ "synthetic-value-348",+ ],+ synthetic_field_036: 1,+ type: "string",+ },+ },+ required: ["synthetic_field_060"],+ type: "object",+ },+ type: "function",+ },+ {+ name: "synthetic_tool_007",+ description: "Synthetic description 349.",+ inputSchema: {+ additionalProperties: false,+ synthetic_field_021: [+ {+ synthetic_field_022: "synthetic_id_002",+ status: "synthetic-value-350",+ },+ ],+ properties: {+ synthetic_field_022: {+ description: "Synthetic description 351.",+ format: "synthetic-value-352",+ type: "string",+ },+ status: {+ description: "Synthetic description 353.",+ synthetic_field_021: ["synthetic-value-354"],+ anyOf: [+ {+ synthetic_field_038: "synthetic-value-355",+ },+ ],+ },+ },+ required: ["synthetic_field_022", "synthetic_field_061"],+ type: "object",+ synthetic_field_042: {+ synthetic_field_062: {+ enum: [+ "synthetic-value-356",+ "synthetic-value-357",+ "synthetic-value-358",+ ],+ type: "string",+ },+ },+ },+ type: "function",+ },+ {+ name: "synthetic_tool_008",+ description: "Synthetic description 359.",+ inputSchema: {+ additionalProperties: false,+ properties: {},+ type: "object",+ },+ type: "function",+ },+ {+ name: "synthetic_tool_009",+ description: "Synthetic description 360.",+ inputSchema: {+ additionalProperties: false,+ synthetic_field_021: [+ {+ synthetic_field_063: [+ {+ description: "Synthetic description 361.",+ synthetic_field_034: "synthetic-value-362",+ },+ {+ description: "Synthetic description 363.",+ synthetic_field_034: "synthetic-value-364",+ },+ ],+ synthetic_field_022: "synthetic_id_002",+ },+ ],+ properties: {+ synthetic_field_022: {+ description: "Synthetic description 365.",+ format: "synthetic-value-366",+ type: "string",+ },+ synthetic_field_063: {+ description: "Synthetic description 367.",+ items: {+ synthetic_field_038: "synthetic-value-368",+ },+ synthetic_field_064: 5,+ type: "synthetic-value-369",+ },+ },+ required: ["synthetic_field_022", "synthetic_field_063"],+ type: "object",+ synthetic_field_042: {+ synthetic_field_065: {+ additionalProperties: false,+ properties: {+ synthetic_field_034: {+ description: "Synthetic description 370.",+ synthetic_field_035: 255,+ type: "string",+ },+ description: {+ description: "Synthetic description 371.",+ type: "string",+ },+ },+ required: ["synthetic_field_034", "synthetic_field_066"],+ type: "object",+ },+ },+ },+ type: "function",+ },+ {+ name: "synthetic_tool_010",+ description: "Synthetic description 372.",+ inputSchema: {+ additionalProperties: false,+ synthetic_field_021: [+ {+ synthetic_field_022: "synthetic_id_002",+ synthetic_field_067: [+ {+ synthetic_field_068: "synthetic-value-373",+ required: true,+ type: "boolean",+ },+ {+ synthetic_field_068: "synthetic-value-374",+ required: false,+ type: "text",+ },+ ],+ },+ ],+ properties: {+ synthetic_field_022: {+ description: "Synthetic description 375.",+ format: "synthetic-value-376",+ type: "string",+ },+ synthetic_field_067: {+ description: "Synthetic description 377.",+ items: {+ synthetic_field_038: "synthetic-value-378",+ },+ type: "synthetic-value-379",+ },+ },+ required: ["synthetic_field_022", "synthetic_field_067"],+ type: "object",+ synthetic_field_042: {+ synthetic_field_069: {+ additionalProperties: false,+ properties: {+ synthetic_field_068: {+ description: "Synthetic description 380.",+ synthetic_field_036: 1,+ type: "string",+ },+ type: {+ description: "Synthetic description 381.",+ enum: ["text", "boolean", "synthetic-value-382"],+ type: "string",+ },+ required: {+ description: "Synthetic description 383.",+ type: "boolean",+ },+ },+ required: [+ "synthetic_field_068",+ "synthetic_field_070",+ "synthetic_field_071",+ ],+ type: "object",+ },+ },+ },+ type: "function",+ },+ {+ name: "synthetic_tool_011",+ description: "Synthetic description 384.",+ inputSchema: {+ additionalProperties: false,+ properties: {+ synthetic_field_072: {+ description: "Synthetic description 385.",+ synthetic_field_036: 1,+ type: "string",+ },+ },+ required: ["synthetic_field_072"],+ type: "object",+ },+ type: "function",+ },+ ],+ },+} satisfies NormalizedIOFixture;diff --git a/packages/shared/src/utils/normalized-io/conventions/providers/pydantic-ai/index.ts b/packages/shared/src/utils/normalized-io/conventions/providers/pydantic-ai/index.tsnew file mode 100644index 0000000000..9170dda977--- /dev/null+++ b/packages/shared/src/utils/normalized-io/conventions/providers/pydantic-ai/index.ts@@ -0,0 +1,52 @@+import { claimed, unmatched } from "../..";+import { parseRecord } from "../../../core/utils/json";+import {+ toolDefinition,+ toolDefinitionProviderMetadata,+} from "../../../core/normalize/tool-definitions";+import type {+ IOConvention,+ ToolDefinitionCarrier,+ ToolDefinitionSource,+} from "../../io-convention";++/**+ * Pydantic AI convention. Currently tool-definition discovery only: Pydantic+ * AI logs its request config as a `model_request_parameters` span attribute+ * (possibly JSON-encoded) with tool declarations under `function_tools`.+ */+function pydanticAiToolDefinitionSources(+ carrier: ToolDefinitionCarrier,+): ToolDefinitionSource[] {+ const modelRequestParameters = parseRecord(+ carrier.metadataAttributes?.model_request_parameters,+ );+ const functionTools = modelRequestParameters?.function_tools;+ if (functionTools === undefined) return [];++ return [+ {+ sourceKey: "model_request_parameters.function_tools",+ value: functionTools,+ options: { allowToolMap: true },+ },+ ];+}++export const pydanticAiProvider = {+ name: "pydantic-ai",+ // Pydantic AI tool declarations: { name, description,+ // parameters_json_schema }.+ tryNormalizeToolDefinition: (value: Record<string, unknown>) => {+ if (value.parameters_json_schema === undefined) return unmatched;+ const definition = toolDefinition({+ name: value.name,+ description: value.description,+ inputSchema: value.parameters_json_schema,+ type: value.type,+ providerMetadata: toolDefinitionProviderMetadata(value, value),+ });+ return definition ? claimed(definition) : unmatched;+ },+ collectToolDefinitionSources: pydanticAiToolDefinitionSources,+} satisfies IOConvention;diff --git a/packages/shared/src/utils/normalized-io/conventions/providers/pydantic-ai/pydantic-ai.test.ts b/packages/shared/src/utils/normalized-io/conventions/providers/pydantic-ai/pydantic-ai.test.tsnew file mode 100644index 0000000000..74b93730f1--- /dev/null+++ b/packages/shared/src/utils/normalized-io/conventions/providers/pydantic-ai/pydantic-ai.test.ts@@ -0,0 +1,16 @@+import { describe, expect, it } from "vitest";++import { normalizeSpanIO } from "../../../parser";+import { pydanticAiProductionShapeFixture } from "./fixtures";++describe("Pydantic AI normalized I/O", () => {+ it.each([pydanticAiProductionShapeFixture])(+ "$name",+ ({ spanIO, expected }) => {+ expect(normalizeSpanIO(spanIO)).toEqual({+ ...expected,+ span: spanIO,+ });+ },+ );+});diff --git a/packages/shared/src/utils/normalized-io/conventions/providers/semantic-kernel/fixtures.ts b/packages/shared/src/utils/normalized-io/conventions/providers/semantic-kernel/fixtures.tsnew file mode 100644index 0000000000..77d3db6323--- /dev/null+++ b/packages/shared/src/utils/normalized-io/conventions/providers/semantic-kernel/fixtures.ts@@ -0,0 +1,44 @@+import type { NormalizedIOFixture } from "../fixture-types";++/** Synthetic Semantic Kernel case adapted from the playground suite. */+export const semanticKernelEventContentFixture = {+ name: "unwraps Semantic Kernel event content",+ spanIO: {+ input: [+ {+ role: "user",+ "gen_ai.event.content": JSON.stringify({+ role: "user",+ content: "What is the weather?",+ tool_calls: [],+ }),+ },+ ],+ output: {+ "gen_ai.event.content": JSON.stringify({+ message: {+ role: "Assistant",+ content: "The weather is mild.",+ },+ }),+ },+ metadata: {+ scope: { name: "Microsoft.SemanticKernel.Diagnostics", version: "" },+ },+ },+ expected: {+ messages: [+ {+ role: "user",+ parts: [{ type: "text", text: "What is the weather?" }],+ source: "input",+ },+ {+ role: "assistant",+ parts: [{ type: "text", text: "The weather is mild." }],+ source: "output",+ },+ ],+ toolDefinitions: [],+ },+} satisfies NormalizedIOFixture;diff --git a/packages/shared/src/utils/normalized-io/conventions/providers/semantic-kernel/index.ts b/packages/shared/src/utils/normalized-io/conventions/providers/semantic-kernel/index.tsnew file mode 100644index 0000000000..338135ce6d--- /dev/null+++ b/packages/shared/src/utils/normalized-io/conventions/providers/semantic-kernel/index.ts@@ -0,0 +1,31 @@+import { claimed, unmatched } from "../../io-convention";+import { asRecord, parseRecord } from "../../../core/utils/json";+import type { NormalizedMessage } from "../../../types";+import type {+ ConventionResult,+ IOConvention,+ MessageEnvelopeContext,+} from "../../io-convention";++/**+ * Semantic Kernel convention. SK logs whole messages as OTel GenAI event+ * payloads under a `gen_ai.event.content` key (a JSON string); the actual+ * message is nested under `message` or is the decoded payload itself.+ */+function unwrapSemanticKernelEnvelope(+ value: Record<string, unknown>,+ fallbackRole: "user" | "assistant",+ ctx: MessageEnvelopeContext,+): ConventionResult<NormalizedMessage> {+ const content = parseRecord(value["gen_ai.event.content"]);+ if (!content) return unmatched;++ return claimed(+ ctx.normalizeMessage(asRecord(content.message) ?? content, fallbackRole),+ );+}++export const semanticKernelProvider = {+ name: "semantic-kernel",+ tryUnwrapMessage: unwrapSemanticKernelEnvelope,+} satisfies IOConvention;diff --git a/packages/shared/src/utils/normalized-io/conventions/providers/semantic-kernel/semantic-kernel.test.ts b/packages/shared/src/utils/normalized-io/conventions/providers/semantic-kernel/semantic-kernel.test.tsnew file mode 100644index 0000000000..0596da200e--- /dev/null+++ b/packages/shared/src/utils/normalized-io/conventions/providers/semantic-kernel/semantic-kernel.test.ts@@ -0,0 +1,16 @@+import { describe, expect, it } from "vitest";++import { normalizeSpanIO } from "../../../parser";+import { semanticKernelEventContentFixture } from "./fixtures";++describe("Semantic Kernel normalized I/O", () => {+ it.each([semanticKernelEventContentFixture])(+ "$name",+ ({ spanIO, expected }) => {+ expect(normalizeSpanIO(spanIO)).toEqual({+ ...expected,+ span: spanIO,+ });+ },+ );+});diff --git a/packages/shared/src/utils/normalized-io/conventions/registry.test.ts b/packages/shared/src/utils/normalized-io/conventions/registry.test.tsnew file mode 100644index 0000000000..4656baa086--- /dev/null+++ b/packages/shared/src/utils/normalized-io/conventions/registry.test.ts@@ -0,0 +1,67 @@+import { existsSync, readdirSync } from "node:fs";+import { join } from "node:path";++import { describe, expect, it } from "vitest";++import { registeredProviders } from "./index";++describe("provider convention registry", () => {+ it("registers every provider directory in conventions/providers/", () => {+ // Registration is one entry in the ordered registry in index.ts β this+ // catches the forgotten entry when a new provider directory is added.+ const providersDir = join(__dirname, "providers");+ const providerDirectories = readdirSync(providersDir, {+ withFileTypes: true,+ }).filter(+ (entry) =>+ entry.isDirectory() &&+ existsSync(join(providersDir, entry.name, "index.ts")),+ );++ expect(registeredProviders).toHaveLength(providerDirectories.length);+ });++ it("uses unique provider names", () => {+ const names = registeredProviders.map((provider) => provider.name);+ expect(new Set(names).size).toBe(names.length);+ });++ it("keeps typed part vocabularies disjoint except for guarded media types", () => {+ const owners = new Map<string, string>();+ const guardedTypes = new Set(["file", "image"]);++ for (const provider of registeredProviders) {+ for (const type of Object.keys(provider.typedParts ?? {})) {+ const previousOwner = owners.get(type);+ if (!guardedTypes.has(type)) {+ expect(+ previousOwner,+ `typed part "${type}" is declared by both ${previousOwner} and ${provider.name}`,+ ).toBeUndefined();+ }+ owners.set(type, provider.name);+ }+ }+ });++ it("maps overlapping finish-reason vocabulary identically across providers", () => {+ // Any raw value claimed by several providers has to map to the same+ // canonical type, so registry order cannot change the result.+ const seen = new Map<string, { provider: string; type: string }>();++ for (const provider of registeredProviders) {+ for (const [raw, type] of Object.entries(+ provider.finishReasonTypeByRaw ?? {},+ )) {+ const previous = seen.get(raw);+ if (previous) {+ expect(+ { raw, type, agreesWith: previous },+ `"${raw}" maps to "${type}" in ${provider.name} but "${previous.type}" in ${previous.provider}`,+ ).toMatchObject({ type: previous.type });+ }+ seen.set(raw, { provider: provider.name, type });+ }+ }+ });+});diff --git a/packages/shared/src/utils/normalized-io/core/accumulator/collect.ts b/packages/shared/src/utils/normalized-io/core/accumulator/collect.tsnew file mode 100644index 0000000000..f2d67d75f8--- /dev/null+++ b/packages/shared/src/utils/normalized-io/core/accumulator/collect.ts@@ -0,0 +1,289 @@+import { registeredProviders } from "../../conventions";+import type { MessageSource } from "../../conventions/io-convention";+import { asRecord, parseIfString, parseRecord } from "../utils/json";+import type { NormalizedMessage, NormalizedMessagePart } from "../../types";+import { addMessage, addToolDefinitionValue } from "./helpers";+import type { NormalizedIOAccumulator } from "./interface";+import type { ParserContext } from "../parser-context";+import {+ normalizePart,+ normalizeMessage,+ normalizeFinishReason,+} from "../normalize";+import { isMessageLike, isToolDefinitionMessage } from "../utils/format";++export type ParsedIOValue = {+ value: unknown;+ record?: Record<string, unknown>;+ messages?: unknown[];+};++function collectToolDefinitionsFromRecord(+ record: Record<string, unknown>,+ accumulator: NormalizedIOAccumulator,+): void {+ addToolDefinitionValue(accumulator, record.tools, {+ allowProviderToolWithoutName: true,+ allowToolMap: true,+ });++ if (isToolDefinitionMessage(record)) {+ addToolDefinitionValue(accumulator, record.content);+ }+}++function collectRootToolDefinitions(+ root: Record<string, unknown>,+ accumulator: NormalizedIOAccumulator,+): void {+ collectToolDefinitionsFromRecord(root, accumulator);++ // Definitions are independent of the message claim. A record can contain+ // an OpenAI `choices` carrier and, next to it, a framework `messages` or+ // `contents` carrier with additional declarations. Inspect only these+ // known carrier boundaries; do not recursively walk arbitrary payloads.+ for (const key of [+ "messages",+ "choices",+ "candidates",+ "contents",+ "output",+ "new_message",+ ]) {+ const parsed = parseIfString(root[key]);+ const candidate = parseRecord(parsed);+ const values = Array.isArray(parsed)+ ? parsed+ : candidate+ ? [candidate]+ : [];+ for (const value of values) {+ const record = asRecord(value);+ if (!record) continue;+ collectToolDefinitionsFromRecord(record, accumulator);+ for (const nestedKey of ["message", "content", "parts"]) {+ const nested = parseRecord(record[nestedKey]);+ if (nested) collectToolDefinitionsFromRecord(nested, accumulator);+ }+ }+ }++ for (const provider of registeredProviders) {+ const sources = provider.collectToolDefinitionSources?.({ root }) ?? [];+ for (const source of sources) {+ addToolDefinitionValue(accumulator, source.value, source.options);+ }+ }+}++function collectMetadataToolDefinitions(+ metadata: Record<string, unknown>,+ accumulator: NormalizedIOAccumulator,+): void {+ addToolDefinitionValue(accumulator, metadata.tools);++ const attributes = parseRecord(metadata.attributes);+ if (!attributes) return;++ addToolDefinitionValue(accumulator, attributes.tools);+ for (const provider of registeredProviders) {+ const sources =+ provider.collectToolDefinitionSources?.({+ metadataAttributes: attributes,+ }) ?? [];+ for (const source of sources) {+ addToolDefinitionValue(accumulator, source.value, source.options);+ }+ }+}++function collectMessageSequence(+ values: unknown[],+ fallbackRole: "user" | "assistant",+ parserContext: ParserContext,+ messages: NormalizedMessage[],+ accumulator: NormalizedIOAccumulator,+): void {+ const { source } = parserContext;+ const standaloneToolCalls: NormalizedMessagePart[] = [];++ const flushStandaloneToolCalls = () => {+ if (standaloneToolCalls.length === 0) return;+ addMessage(+ messages,+ {+ role: "assistant",+ parts: standaloneToolCalls.splice(0),+ source,+ },+ parserContext,+ );+ };++ for (const value of values) {+ const record = asRecord(value);+ if (record) collectToolDefinitionsFromRecord(record, accumulator);++ // OpenAI Responses tool listings are declarations, not conversation+ // content. Keep them side-band without breaking an adjacent call batch.+ if (record?.type === "mcp_list_tools") continue;++ if (record && !isMessageLike(record)) {+ const part = normalizePart(record, parserContext);+ if (part?.type === "tool-call") {+ standaloneToolCalls.push(part);+ continue;+ }+ }++ flushStandaloneToolCalls();+ const message = normalizeMessage(value, fallbackRole, parserContext);+ if (message) addMessage(messages, message, parserContext);+ }++ flushStandaloneToolCalls();+}++function emitRootSource(+ rootSource: MessageSource,+ parserContext: ParserContext,+ messages: NormalizedMessage[],+ accumulator: NormalizedIOAccumulator,+): void {+ if (rootSource.kind === "sequence") {+ collectMessageSequence(+ rootSource.values,+ rootSource.fallbackRole,+ parserContext,+ messages,+ accumulator,+ );+ return;+ }++ const message = normalizeMessage(+ rootSource.value,+ rootSource.fallbackRole,+ parserContext,+ );+ if (!message) return;++ const finishReason =+ (rootSource.finishReasonCarrier+ ? normalizeFinishReason(rootSource.finishReasonCarrier)+ : undefined) ?? message.finishReason;+ addMessage(+ messages,+ {+ ...message,+ role: rootSource.roleOverride ?? message.role,+ ...(finishReason ? { finishReason } : {}),+ },+ parserContext,+ );+}++function collectRecordMessages(+ parsedValue: ParsedIOValue,+ parserContext: ParserContext,+ accumulator: NormalizedIOAccumulator,+): void {+ const record = parsedValue.record;+ if (!record) return;++ const { source } = parserContext;+ const fallbackRole = source === "input" ? "user" : "assistant";++ const providerClaims = registeredProviders.map((provider) => ({+ provider,+ sources: provider.claimMessages?.(record, source) ?? [],+ systemMessage: provider.getSystemMessage?.(record, source),+ }));+ const selectedClaim = providerClaims.find(+ ({ sources }) => sources.length > 0,+ );++ if (selectedClaim) {+ parserContext.preferredProvider = selectedClaim.provider;+ }++ const messages: NormalizedMessage[] = [];+ const claimedSources = selectedClaim?.sources;++ if (claimedSources && claimedSources.length > 0) {+ for (const rootSource of claimedSources) {+ emitRootSource(rootSource, parserContext, messages, accumulator);+ }+ } else if (parsedValue.messages) {+ collectMessageSequence(+ parsedValue.messages,+ fallbackRole,+ parserContext,+ messages,+ accumulator,+ );+ } else {+ const recordMessage = normalizeMessage(record, fallbackRole, parserContext);+ if (recordMessage) addMessage(messages, recordMessage, parserContext);+ }++ // System instructions supplement the selected input conversation. Defer+ // them until after message normalization so an existing system message+ // suppresses every top-level system sidecar.+ if (source === "input" && !parserContext.hasSystemMessage) {+ const systemMessages: NormalizedMessage[] = [];+ for (const { systemMessage } of providerClaims) {+ if (systemMessage) {+ emitRootSource(+ systemMessage,+ parserContext,+ systemMessages,+ accumulator,+ );+ }+ if (parserContext.hasSystemMessage) break;+ }+ messages.unshift(...systemMessages);+ }++ accumulator.messages.push(...messages);+}++export function collectMetadata(+ parsedValue: ParsedIOValue,+ accumulator: NormalizedIOAccumulator,+): void {+ if (parsedValue.record) {+ collectMetadataToolDefinitions(parsedValue.record, accumulator);+ }+ return;+}++export function collectIO(+ parsedValue: ParsedIOValue,+ context: ParserContext,+ accumulator: NormalizedIOAccumulator,+): void {+ const { source } = context;+ const fallbackRole = source === "input" ? "user" : "assistant";++ if (Array.isArray(parsedValue.value)) {+ collectMessageSequence(+ parsedValue.messages ?? parsedValue.value,+ fallbackRole,+ context,+ accumulator.messages,+ accumulator,+ );+ return;+ }++ if (parsedValue.record) {+ collectRootToolDefinitions(parsedValue.record, accumulator);+ collectRecordMessages(parsedValue, context, accumulator);+ return;+ }++ const message = normalizeMessage(parsedValue.value, fallbackRole, context);+ if (message) addMessage(accumulator.messages, message, context);+}diff --git a/packages/shared/src/utils/normalized-io/core/accumulator/helpers.ts b/packages/shared/src/utils/normalized-io/core/accumulator/helpers.tsnew file mode 100644index 0000000000..c894e0339d--- /dev/null+++ b/packages/shared/src/utils/normalized-io/core/accumulator/helpers.ts@@ -0,0 +1,98 @@+import { normalizeToolDefinitionValue } from "../normalize/tool-definitions";+import {+ NormalizedMessage,+ NormalizedMessagePart,+ ToolDefinition,+} from "../../types";+import type { NormalizedIOAccumulator } from "./interface";+import { ToolDefinitionOptions } from "../../conventions/io-convention";+import type { ParserContext } from "../parser-context";+import { getToolCallKeyForPart } from "../normalize/part";++export function addToolDefinitionValue(+ accumulator: NormalizedIOAccumulator,+ value: unknown,+ options: ToolDefinitionOptions = {},+): void {+ for (const definition of normalizeToolDefinitionValue(value, options)) {+ addToolDefinition(accumulator, definition);+ }+}++function getToolCallKey(part: NormalizedMessagePart): string | undefined {+ if (part.type !== "tool-call") return undefined;++ // Keep legacy identity (including raw argument spelling) out of the public+ // part shape while still using it for output-side compatibility deduping.+ const rawKey = getToolCallKeyForPart(part);+ if (rawKey) return rawKey;++ const id = part.toolCallId;+ if (typeof id === "string" && id.length > 0) return `id:${id}`;++ try {+ return `value:${String(part.toolName)}:${JSON.stringify(part.input)}`;+ } catch {+ return undefined;+ }+}++export function addMessage(+ messages: NormalizedMessage[],+ message: NormalizedMessage,+ context: ParserContext,+): void {+ // Input is conversation history and must preserve repeated calls. Output+ // is projected into the legacy tool columns, where duplicate+ // representations are removed as they are encountered.+ const parts =+ context.source === "output"+ ? message.parts.filter((part) => {+ const key = getToolCallKey(part);+ if (!key) return true;+ if (context.toolCallKeys.has(key)) return false;++ context.toolCallKeys.add(key);+ return true;+ })+ : message.parts;++ if (parts.length > 0) {+ if (context.source === "input" && message.role === "system") {+ context.hasSystemMessage = true;+ }+ messages.push({ ...message, parts });+ }+}++function addToolDefinition(+ accumulator: NormalizedIOAccumulator,+ definition: ToolDefinition,+): void {+ const existingIndex = accumulator.toolDefinitionIndexByName.get(+ definition.name,+ );++ if (existingIndex === undefined) {+ accumulator.toolDefinitionIndexByName.set(+ definition.name,+ accumulator.toolDefinitions.length,+ );+ accumulator.toolDefinitions.push(definition);+ return;+ }++ const existing = accumulator.toolDefinitions[existingIndex];+ if (!existing) return;++ accumulator.toolDefinitions[existingIndex] = {+ name: existing.name,+ description: existing.description ?? definition.description,+ inputSchema: existing.inputSchema ?? definition.inputSchema,+ type: existing.type ?? definition.type,+ providerMetadata:+ existing.providerMetadata && definition.providerMetadata+ ? { ...definition.providerMetadata, ...existing.providerMetadata }+ : (existing.providerMetadata ?? definition.providerMetadata),+ };+}diff --git a/packages/shared/src/utils/normalized-io/core/accumulator/index.ts b/packages/shared/src/utils/normalized-io/core/accumulator/index.tsnew file mode 100644index 0000000000..11c43f6eeb--- /dev/null+++ b/packages/shared/src/utils/normalized-io/core/accumulator/index.ts@@ -0,0 +1,2 @@+export * from "./collect";+export * from "./interface";diff --git a/packages/shared/src/utils/normalized-io/core/accumulator/interface.ts b/packages/shared/src/utils/normalized-io/core/accumulator/interface.tsnew file mode 100644index 0000000000..f19500fec9--- /dev/null+++ b/packages/shared/src/utils/normalized-io/core/accumulator/interface.ts@@ -0,0 +1,15 @@+import { NormalizedMessage, ToolDefinition } from "../../types";++export type NormalizedIOAccumulator = {+ messages: NormalizedMessage[];+ toolDefinitions: ToolDefinition[];+ toolDefinitionIndexByName: Map<string, number>;+};++export function createAccumulator(): NormalizedIOAccumulator {+ return {+ messages: [],+ toolDefinitions: [],+ toolDefinitionIndexByName: new Map(),+ };+}diff --git a/packages/shared/src/utils/normalized-io/core/normalize/finish-reason.ts b/packages/shared/src/utils/normalized-io/core/normalize/finish-reason.tsnew file mode 100644index 0000000000..8484310e4e--- /dev/null+++ b/packages/shared/src/utils/normalized-io/core/normalize/finish-reason.ts@@ -0,0 +1,34 @@+import { registeredProviders } from "../../conventions";+import { FinishReason } from "../../types";+import { optionalString, ownLookup } from "../utils/json";++/**+ * Read the finish/stop carrier off a record (message, choice/candidate, or+ * envelope event) and canonicalize it: anthropic carries `stop_reason`,+ * openai/gemini carry `finish_reason`/`finishReason`, langchain nests both+ * under `response_metadata` (passed as the second carrier).+ */+export function normalizeFinishReason(+ data: Record<string, unknown>,+ responseMetadata?: Record<string, unknown>,+): FinishReason | undefined {+ const raw =+ data.stop_reason ?? // anthropic+ data.finish_reason ??+ data.finishReason ??+ responseMetadata?.finish_reason ?? // langchain+ responseMetadata?.stop_reason; // langchain++ const value = optionalString(raw);+ if (!value) return undefined;++ // Lookups are lowercased (Gemini reports uppercase values). Registry order+ // must never matter: overlapping vocabulary across providers maps to the+ // same canonical value (asserted by registry.test.ts).+ const lowered = value.toLowerCase();+ const providerType = registeredProviders+ .map((provider) => ownLookup(provider.finishReasonTypeByRaw, lowered))+ .find((type) => type !== undefined);++ return { type: providerType ?? "unknown", raw: value };+}diff --git a/packages/shared/src/utils/normalized-io/core/normalize/index.ts b/packages/shared/src/utils/normalized-io/core/normalize/index.tsnew file mode 100644index 0000000000..5c03017ad0--- /dev/null+++ b/packages/shared/src/utils/normalized-io/core/normalize/index.ts@@ -0,0 +1,4 @@+export * from "./message";+export * from "./part";+export * from "./role";+export * from "./finish-reason";diff --git a/packages/shared/src/utils/normalized-io/core/normalize/message-parts/fallback.ts b/packages/shared/src/utils/normalized-io/core/normalize/message-parts/fallback.tsnew file mode 100644index 0000000000..8ad2c37e2b--- /dev/null+++ b/packages/shared/src/utils/normalized-io/core/normalize/message-parts/fallback.ts@@ -0,0 +1,94 @@+import { NormalizedMessagePart } from "../../../types";+import { asRecord, toJsonValue } from "../../utils/json";+import { toolCallPart } from "./tool-calls";++// `type` strings that count as tool-call evidence for the flat sniff below,+// by owner. The flattening itself has no single emitter (OTel GenAI event+// payloads, koog/Traceloop conversions, frameworks that unwrap the OpenAI+// `function` wrapper) β only the markers do.+const FLAT_TOOL_CALL_TYPE_MARKERS = new Set([+ "function",+ "function_call", // openai+ "tool-call", // ai sdk+ "tool_call", // otel genai, langchain+ "tool_use",+ "server_tool_use",+ "mcp_tool_use", // anthropic+]);++/**+ * Shape-sniffed tool calls without a matched typed/untyped case:+ * OpenAI's `{function: {name, arguments}}` wrapper, Responses' flat+ * `{call_id, name, arguments}`, AI SDK's `{toolName, input | args}`, and the+ * bare `{name, arguments, <id | index | marker>}` shape loose instrumentation+ * emits. Recognition and extraction are one function: `undefined` means the+ * value is not call-shaped (fallback continues); `null` means call-shaped+ * but unconstructible (dropped). Guarded β bare `{name, arguments}` alone+ * never matches. This is the deliberate field-vocabulary join for shapes+ * whose dialect is unknown; owner comments per field.+ */+function sniffLooseToolCall(+ value: Record<string, unknown>,+): NormalizedMessagePart | null | undefined {+ if (value.type === "tool-result") return undefined;++ const functionCall = asRecord(value.function);+ const isCallShaped =+ Boolean(functionCall?.name && "arguments" in functionCall) || // openai chat completions+ Boolean(value.call_id && value.name && "arguments" in value) || // openai responses+ Boolean(+ value.toolName && // ai sdk+ (value.type === "tool-call" ||+ value.type === "tool_call" ||+ "input" in value ||+ "args" in value),+ ) ||+ Boolean(+ value.name &&+ "arguments" in value &&+ ("id" in value ||+ "index" in value ||+ FLAT_TOOL_CALL_TYPE_MARKERS.has(String(value.type))),+ );+ if (!isCallShaped) return undefined;++ return toolCallPart({+ toolCallId:+ value.toolCallId /* ai sdk */ ??+ value.call_id /* openai responses */ ??+ value.id,+ toolName:+ value.toolName /* ai sdk */ ??+ value.name /* anthropic, gemini, openai responses */ ??+ functionCall?.name,+ input:+ value.input /* anthropic, ai sdk */ ??+ value.arguments /* openai, otel */ ??+ value.args /* ai sdk, langchain */ ??+ functionCall?.arguments,+ toolType:+ typeof value.type === "string"+ ? value.type+ : functionCall+ ? "function"+ : undefined,+ providerExecuted: value.providerExecuted, // ai sdk+ });+}++export function normalizeFallbackPart(+ value: Record<string, unknown>,+): NormalizedMessagePart | null {+ const sniffed = sniffLooseToolCall(value);+ if (sniffed !== undefined) return sniffed;++ if (typeof value.type !== "string") {+ return { type: "data", value: toJsonValue(value) };+ }++ return {+ type: "custom",+ kind: value.type,+ value: toJsonValue(value),+ };+}diff --git a/packages/shared/src/utils/normalized-io/core/normalize/message-parts/media.ts b/packages/shared/src/utils/normalized-io/core/normalize/message-parts/media.tsnew file mode 100644index 0000000000..dd7405e4cc--- /dev/null+++ b/packages/shared/src/utils/normalized-io/core/normalize/message-parts/media.ts@@ -0,0 +1,125 @@+import {+ MEDIA_REFERENCE_PATTERN,+ MediaReferenceStringSchema,+} from "../../../../../utils/IORepresentation/chatML/types";+import { compact, toProviderMetadata } from "../../utils/json";+import type { FilePart, NormalizedMessagePart } from "../../../types";++/**+ * Generic media and file builders: Langfuse media-token parsing, data-URI+ * prefix sniffing (RFC 2397), and plain url parts. No provider vocabulary β+ * per-dialect file builders live in their convention files.+ */++export type ParsedMediaReference = {+ type: string;+ id: string;+ source: string;+ referenceString: string;+};++// Cheap substring pre-check so token-free strings (nearly all) never pay+// for regex scans.+const MEDIA_TOKEN_HINT = "@@@langfuseMedia";++export function parseMediaReference(+ value: unknown,+): ParsedMediaReference | undefined {+ if (typeof value !== "string" || !value.includes(MEDIA_TOKEN_HINT)) {+ return undefined;+ }++ const matches = value.match(MEDIA_REFERENCE_PATTERN) ?? [];+ if (matches.length !== 1 || matches[0] !== value) return undefined;++ const parsed = MediaReferenceStringSchema.safeParse(value);+ return parsed.success ? parsed.data : undefined;+}++export function filePartFromMediaReference(+ reference: ParsedMediaReference,+ extras?: Record<string, unknown>,+): FilePart {+ return compact<FilePart>({+ type: "file",+ mediaType: reference.type,+ content: { kind: "reference", id: reference.id },+ providerMetadata: toProviderMetadata({+ source: reference.source,+ ...extras,+ }),+ });+}++/**+ * Split a string into interleaved text and file parts, one file part per+ * embedded media reference token β strings frequently carry several.+ */+export function normalizeMediaPartsFromString(+ value: string,+): NormalizedMessagePart[] {+ if (!value.includes(MEDIA_TOKEN_HINT)) return [{ type: "text", text: value }];++ const parts: NormalizedMessagePart[] = [];+ let lastIndex = 0;++ for (const match of value.matchAll(MEDIA_REFERENCE_PATTERN)) {+ const parsed = parseMediaReference(match[0]);+ if (!parsed) continue; // stays part of the surrounding text++ const before = value.slice(lastIndex, match.index);+ if (before.trim().length > 0) parts.push({ type: "text", text: before });+ parts.push(filePartFromMediaReference(parsed));+ lastIndex = match.index + match[0].length;+ }++ if (parts.length === 0) return [{ type: "text", text: value }];++ const rest = value.slice(lastIndex);+ if (rest.trim().length > 0) parts.push({ type: "text", text: rest });+ return parts;+}++/**+ * Read the media type a data-URI declares in its prefix, without touching+ * the payload. Raw data-URIs only reach stored IO when upstream media+ * processing skipped or failed; decoding them stays the media pipeline's job.+ */+export function mediaTypeFromDataUri(url: string): string | undefined {+ if (!url.startsWith("data:")) return undefined;+ const end = url.slice(5).search(/[;,]/);+ return end > 0 ? url.slice(5, 5 + end) : undefined;+}++export type UrlFilePartOptions = {+ /** Explicit media type declared by the source (wins over sniffing). */+ mediaType?: string;+ /** Modality wildcard to apply when nothing better is known. */+ fallbackMediaType?: string;+ extras?: Record<string, unknown>;+};++/**+ * Media-token check -> data-URI prefix sniff -> plain url. Shared by every+ * url-bearing media field (chat image_url, Responses input_image, Anthropic+ * url sources).+ */+export function filePartFromUrl(+ url: string,+ options: UrlFilePartOptions = {},+): FilePart {+ const reference = parseMediaReference(url);+ if (reference) return filePartFromMediaReference(reference, options.extras);++ return compact<FilePart>({+ type: "file",+ mediaType:+ options.mediaType ??+ mediaTypeFromDataUri(url) ?? // Q: can I not remove this? vercel ai specific+ options.fallbackMediaType,+ content: { kind: "url", url },+ providerMetadata: options.extras+ ? toProviderMetadata(options.extras)+ : undefined,+ });+}diff --git a/packages/shared/src/utils/normalized-io/core/normalize/message-parts/reasoning.ts b/packages/shared/src/utils/normalized-io/core/normalize/message-parts/reasoning.tsnew file mode 100644index 0000000000..8cd39f0463--- /dev/null+++ b/packages/shared/src/utils/normalized-io/core/normalize/message-parts/reasoning.ts@@ -0,0 +1,26 @@+import type { NormalizedMessagePart, ReasoningPart } from "../../../types";+import { compact, toJsonValue } from "../../utils/json";++/** String payloads become visible reasoning text; everything else is data. */+export function reasoningPart(+ payload: unknown,+ signature?: string,+): ReasoningPart {+ return {+ type: "reasoning",+ content:+ typeof payload === "string"+ ? compact({ kind: "text" as const, text: payload, signature })+ : { kind: "data", value: toJsonValue(payload) },+ };+}++/** Universal reasoning-text block: `reasoning`/`reasoning_text`/`summary_text`+ * all carry their payload under one of text/content/thinking/summary. */+export function normalizeReasoningTextPart(+ value: Record<string, unknown>,+): NormalizedMessagePart {+ const reasoning =+ value.text ?? value.content ?? value.thinking ?? value.summary;+ return reasoningPart(reasoning);+}diff --git a/packages/shared/src/utils/normalized-io/core/normalize/message-parts/text.ts b/packages/shared/src/utils/normalized-io/core/normalize/message-parts/text.tsnew file mode 100644index 0000000000..2a449eb4f0--- /dev/null+++ b/packages/shared/src/utils/normalized-io/core/normalize/message-parts/text.ts@@ -0,0 +1,39 @@+import { registeredProviders } from "../../../conventions";+import type { JsonValue, NormalizedMessagePart } from "../../../types";+import { optionalString, parseArray, toJsonValue } from "../../utils/json";+import { reasoningPart } from "./reasoning";++/** Citation carriers are provider vocabulary (`citationKeys`); the first+ * non-empty carrier lifts verbatim into `providerMetadata.citations`. */+export function extractCitations(+ value: Record<string, unknown>,+): JsonValue | undefined {+ for (const provider of registeredProviders) {+ for (const key of provider.citationKeys ?? []) {+ const citations = parseArray(value[key]);+ if (citations && citations.length > 0) return toJsonValue(citations);+ }+ }+ return undefined;+}++/** Universal text block: OpenAI Responses `input_text`/`output_text` share+ * this shape with the generic `text` type; Gemini's typed thought flag is+ * checked the same way regardless of which of the three type strings matched. */+export function normalizeTextPart(+ value: Record<string, unknown>,+): NormalizedMessagePart {+ const text = optionalString(value.text ?? value.content) ?? "";+ if (value.thought === true) {+ return reasoningPart(+ text,+ optionalString(value.thoughtSignature ?? value.thought_signature),+ );+ }+ const citations = extractCitations(value);+ return {+ type: "text",+ text,+ ...(citations ? { providerMetadata: { citations } } : {}),+ };+}diff --git a/packages/shared/src/utils/normalized-io/core/normalize/message-parts/tool-calls.ts b/packages/shared/src/utils/normalized-io/core/normalize/message-parts/tool-calls.tsnew file mode 100644index 0000000000..27ecbcea96--- /dev/null+++ b/packages/shared/src/utils/normalized-io/core/normalize/message-parts/tool-calls.ts@@ -0,0 +1,64 @@+import type { ToolCallPart } from "../../../types";+import {+ compact,+ parseIfString,+ toJsonValue,+ nullableString,+ toProviderMetadata,+} from "../../utils/json";++/**+ * Canonical tool-part constructors. Callers extract their own dialect's+ * fields and pass them here; the flat-sniff fallback in `fallback.ts` and+ * the otel-genai convention keep deliberately loose extractions for shapes+ * whose dialect is unknown.+ */++export type ToolCallFields = {+ /** Raw id value; coerced via nullableString. */+ toolCallId?: unknown;+ /** Must be a non-empty string, otherwise no part is constructed. */+ toolName: unknown;+ /** Raw arguments payload; one JSON-string boundary is parsed. Defaults {}. */+ input?: unknown;+ toolType?: string;+ /** Kept only when a boolean. */+ providerExecuted?: unknown;+};++export function toolCallPart(fields: ToolCallFields): ToolCallPart | null {+ if (typeof fields.toolName !== "string" || fields.toolName.length === 0) {+ return null;+ }++ return compact<ToolCallPart>({+ type: "tool-call",+ toolCallId: nullableString(fields.toolCallId),+ toolName: fields.toolName,+ input: toJsonValue(parseIfString(fields.input ?? {})),+ toolType: fields.toolType,+ providerExecuted:+ typeof fields.providerExecuted === "boolean"+ ? fields.providerExecuted+ : undefined,+ });+}++/**+ * Provider-executed named calls (OpenAI mcp_call, Anthropic server_tool_use /+ * mcp_tool_use): decorates an already-extracted call with providerExecuted+ * and provider-side extras (server_label, server_name, output, error) in+ * providerMetadata.+ */+export function providerExecutedToolCall(+ part: ToolCallPart | null,+ extras?: Record<string, unknown>,+): ToolCallPart | null {+ if (!part) return null;++ return compact<ToolCallPart>({+ ...part,+ providerExecuted: true,+ providerMetadata: extras ? toProviderMetadata(extras) : undefined,+ });+}diff --git a/packages/shared/src/utils/normalized-io/core/normalize/message-parts/tool-results.ts b/packages/shared/src/utils/normalized-io/core/normalize/message-parts/tool-results.tsnew file mode 100644index 0000000000..bd9d87d2d6--- /dev/null+++ b/packages/shared/src/utils/normalized-io/core/normalize/message-parts/tool-results.ts@@ -0,0 +1,61 @@+import type { ToolResultPart } from "../../../types";+import {+ asRecord,+ optionalString,+ compact,+ nullableString,+ toJsonValue,+ parseIfString,+} from "../../utils/json";++// {type, value} output wrappers originate in the AI SDK but echo through+// loose conversions of every dialect, so unwrapping them is canonical+// builder mechanics β the one deliberate residual in this file. error-*+// marks a failed execution.+const OUTPUT_WRAPPER_TYPES = new Set([+ "text",+ "json",+ "content",+ "error-text",+ "error-json",+ "execution-denied",+]);++type ToolResultFields = {+ /** Raw id value; coerced via nullableString. */+ toolCallId?: unknown;+ /** Raw name value; coerced via optionalString. */+ toolName?: unknown;+ /** Raw payload; the builder unwraps output wrappers, parses one+ * JSON-string boundary, and converts to JsonValue. */+ output?: unknown;+ isError?: boolean;+};++/**+ * Canonical tool-result constructor. Takes already-extracted fields β the+ * caller (a provider handler, or `toolResultPartFromLooseShape` for+ * dialect-less shapes) owns the field vocabulary; this owns only the+ * mechanics every dialect shares.+ */+export function toolResultPart(fields: ToolResultFields): ToolResultPart {+ const wrapper = asRecord(fields.output);+ const wrapperType = optionalString(wrapper?.type);+ const isWrapped =+ wrapper !== undefined &&+ wrapperType !== undefined &&+ "value" in wrapper &&+ OUTPUT_WRAPPER_TYPES.has(wrapperType);+ const wrapperError =+ isWrapped && wrapperType.startsWith("error") ? true : undefined;++ return compact<ToolResultPart>({+ type: "tool-result",+ toolCallId: nullableString(fields.toolCallId),+ toolName: optionalString(fields.toolName),+ output: toJsonValue(+ parseIfString(isWrapped ? wrapper.value : (fields.output ?? null)),+ ),+ isError: fields.isError ?? wrapperError,+ });+}diff --git a/packages/shared/src/utils/normalized-io/core/normalize/message.ts b/packages/shared/src/utils/normalized-io/core/normalize/message.tsnew file mode 100644index 0000000000..e55c741f34--- /dev/null+++ b/packages/shared/src/utils/normalized-io/core/normalize/message.ts@@ -0,0 +1,255 @@+import type {+ MessageEnvelopeContext,+ PartHandlerContext,+ SiblingPartContribution,+ SiblingPartSlot,+} from "../../conventions/io-convention";+import { isMessageLike, isToolDefinitionMessage } from "../utils/format";+import type {+ NormalizedMessage,+ NormalizedMessagePart,+ NormalizedMessageRole,+} from "../../types";+import { asRecord, isRecord, optionalString, parseArray } from "../utils/json";+import { normalizeFinishReason } from "./finish-reason";+import { normalizeMediaPartsFromString } from "./message-parts/media";+import { extractCitations } from "./message-parts/text";+import { normalizePart, normalizePartList } from "./part";+import { coerceRole, normalizeRole } from "./role";+import type { ParserContext } from "../parser-context";+import { providersInOrder } from "../utils/providers";++/** Normalize the content owned by one already-selected message. */+function normalizeMessageContent(+ value: Record<string, unknown>,+ nestedContent: Record<string, unknown> | undefined,+ role: NormalizedMessageRole,+ parserContext: ParserContext,+): NormalizedMessagePart[] {+ const rawParts = Array.isArray(value.parts)+ ? value.parts+ : Array.isArray(value.content)+ ? value.content+ : Array.isArray(nestedContent?.parts)+ ? nestedContent.parts+ : undefined;+ if (rawParts) return normalizePartList(rawParts, parserContext);++ if (typeof value.content === "string" && value.content.length > 0) {+ // Koog can serialize an assistant tool-call batch into a string under a+ // tool-role message. Only treat it as parts when every item is a tool.+ const parsedContent = parseArray(value.content);+ if (parsedContent?.length) {+ const parsedParts = parsedContent.map((part) =>+ normalizePart(part, parserContext),+ );+ if (+ parsedParts.every(+ (part) => part?.type === "tool-call" || part?.type === "tool-result",+ )+ ) {+ return parsedParts.filter((part) => part !== null);+ }+ }++ return normalizeMediaPartsFromString(value.content);+ }++ if (isRecord(value.content)) {+ const part = normalizePart(value.content, parserContext);+ return part ? [part] : [];+ }++ return [];+}++/**+ * Parts and part mutations from fields that live beside `content`. The+ * generic `tool_calls`/`toolCalls` array is universal (OpenAI, LangChain,+ * AI-SDK-flavored logging); everything else folds through+ * `collectSiblingParts`, then message-level citations attach (Chat+ * Completions carries them on the message; Anthropic/Responses carry them+ * per part, handled in `normalize/part.ts`).+ */+function applySiblingFields(+ value: Record<string, unknown>,+ parts: NormalizedMessagePart[],+ parserContext: ParserContext,+): void {+ const partContext: PartHandlerContext = {+ normalizePart: (part) => normalizePart(part, parserContext),+ normalizePartList: (values) => normalizePartList(values, parserContext),+ };+ const contributions: SiblingPartContribution[] = [];+ for (const provider of providersInOrder(parserContext.preferredProvider)) {+ const siblingParts = provider.collectSiblingParts?.(+ value,+ parts,+ partContext,+ );+ if (siblingParts?.length) contributions.push(...siblingParts);+ }++ // One message can expose the same call through native content and one or+ // more framework sibling fields. Claim it in the first carrier only. This+ // is deliberately message-local: identical calls in other turns survive.+ const claimedCarrierByToolCall = new Map<string, string>();+ const claimKey = (part: NormalizedMessagePart): string | undefined => {+ if (part.type !== "tool-call") return undefined;+ if (part.toolCallId) return `id:${part.toolCallId}`;+ try {+ return `value:${part.toolName}:${JSON.stringify(part.input)}`;+ } catch {+ return undefined;+ }+ };+ const claimCarrierParts = (+ carrier: string,+ carrierParts: NormalizedMessagePart[],+ ) =>+ carrierParts.filter((part) => {+ const key = claimKey(part);+ if (!key) return true;+ const claimedCarrier = claimedCarrierByToolCall.get(key);+ if (claimedCarrier && claimedCarrier !== carrier) return false;+ if (!claimedCarrier) claimedCarrierByToolCall.set(key, carrier);+ return true;+ });++ for (const part of parts) {+ const key = claimKey(part);+ if (key && !claimedCarrierByToolCall.has(key)) {+ claimedCarrierByToolCall.set(key, "content");+ }+ }++ const bySlot = (slot: SiblingPartSlot) =>+ contributions+ .filter((contribution) => contribution.slot === slot)+ .flatMap((contribution) =>+ claimCarrierParts(contribution.sourceKey, contribution.parts),+ );++ parts.unshift(...bySlot("before-content"));+ parts.push(...bySlot("after-content"));+ for (const key of ["tool_calls", "toolCalls"]) {+ parts.push(+ ...claimCarrierParts(+ key,+ normalizePartList(parseArray(value[key]) ?? [], parserContext),+ ),+ );+ }+ parts.push(...bySlot("after-tool-calls"));++ const citations = extractCitations(value);+ if (citations) {+ const textPart = parts.find((part) => part.type === "text");+ if (textPart) {+ textPart.providerMetadata = { ...textPart.providerMetadata, citations };+ }+ }+}++export function normalizeMessage(+ value: unknown,+ fallbackRole: "user" | "assistant",+ parserContext: ParserContext,+): NormalizedMessage | null {+ const { source } = parserContext;+ if (typeof value === "string") {+ if (value.length === 0) return null;+ for (const provider of providersInOrder(parserContext.preferredProvider)) {+ const preProcessed = provider.tryPreprocessMessage?.(value);+ if (preProcessed?.matched) {+ return preProcessed.value === null+ ? null+ : normalizeMessage(preProcessed.value, fallbackRole, parserContext);+ }+ }+ const parts = normalizeMediaPartsFromString(value);+ return parts.length > 0 ? { role: fallbackRole, parts, source } : null;+ }+ if (!isRecord(value) || isToolDefinitionMessage(value)) return null;++ const envelopeContext: MessageEnvelopeContext = {+ source,+ normalizeMessage: (nestedValue, nestedFallbackRole) =>+ normalizeMessage(nestedValue, nestedFallbackRole, parserContext),+ isMessageLike,+ normalizeFinishReason,+ };+ for (const provider of providersInOrder(parserContext.preferredProvider)) {+ const unwrapped = provider.tryUnwrapMessage?.(+ value,+ fallbackRole,+ envelopeContext,+ );+ if (unwrapped?.matched) return unwrapped.value;+ }++ // Standalone tool-call/result values (no message keys): normalize once,+ // inspect the result, rather than shape-probing before normalizing.+ const directPart = !isMessageLike(value)+ ? normalizePart(value, parserContext)+ : null;+ if (+ directPart &&+ (directPart.type === "tool-call" || directPart.type === "tool-result")+ ) {+ return {+ role: directPart.type === "tool-result" ? "tool" : "assistant",+ parts: [directPart],+ source,+ };+ }++ const nestedContent = asRecord(value.content);+ const recognizedRole = normalizeRole(value);+ let role =+ recognizedRole ??+ (nestedContent ? normalizeRole(nestedContent) : undefined) ??+ fallbackRole;++ const parts = normalizeMessageContent(+ value,+ nestedContent,+ role,+ parserContext,+ );+ applySiblingFields(value, parts, parserContext);+ role = coerceRole(role, value, parts);++ // Last resort for non-message payloads (function-span args/results, plain+ // records, {text} items): represent them instead of dropping β the trace+ // view renders these as JSON too.+ if (parts.length === 0 && !isMessageLike(value)) {+ const part = normalizePart(value, parserContext);+ if (part) parts.push(part);+ }++ // Choice/candidate-level values are wired in by the accumulator collector.+ const finishReason = normalizeFinishReason(+ value,+ asRecord(value.response_metadata), // langchain+ );++ // Sender identity: an explicit participant name, or β for unrecognized+ // declared roles (e.g. LangGraph putting the agent name in the role field)+ // β the raw role string, so the identity survives the role fallback.+ const declaredRole = optionalString(value.role ?? value.author);+ const senderName =+ optionalString(value.name) ??+ (recognizedRole === undefined ? declaredRole : undefined);++ return parts.length > 0+ ? {+ ...(optionalString(value.id) ? { id: String(value.id) } : {}),+ ...(senderName ? { senderName } : {}),+ role,+ parts,+ ...(finishReason ? { finishReason } : {}),+ source,+ }+ : null;+}diff --git a/packages/shared/src/utils/normalized-io/core/normalize/part.ts b/packages/shared/src/utils/normalized-io/core/normalize/part.tsnew file mode 100644index 0000000000..06e8a5edc7--- /dev/null+++ b/packages/shared/src/utils/normalized-io/core/normalize/part.ts@@ -0,0 +1,314 @@+import { claimed } from "../../conventions";+import type {+ PartHandler,+ PartHandlerContext,+} from "../../conventions/io-convention";+import type { NormalizedMessagePart } from "../../types";+import {+ asRecord,+ isRecord,+ ownLookup,+ remainingProviderMetadata,+} from "../utils/json";+import { normalizeFallbackPart } from "./message-parts/fallback";+import {+ filePartFromMediaReference,+ normalizeMediaPartsFromString,+ parseMediaReference,+} from "./message-parts/media";+import { normalizeReasoningTextPart } from "./message-parts/reasoning";+import { normalizeTextPart } from "./message-parts/text";+import type { ParserContext } from "../parser-context";+import { providersInOrder } from "../utils/providers";++const rawToolCallKeys = new WeakMap<object, string>();++/**+ * Part dispatch. Typed parts are claimed exclusively by one handler; a+ * message may still contain parts from several provider dialects. Shared+ * type names are handled once here, while provider-specific names are looked+ * up in the convention registry.+ */++// `image` and `file` are contested type names: Anthropic/AI-SDK and+// OpenAI/AI-SDK register guarded handlers for them in their own conventions;+// a handler returning `unmatched` falls through to the next provider.+const SHARED_TYPED_PART_HANDLERS: Readonly<Record<string, PartHandler>> = {+ text: (value) => claimed(normalizeTextPart(value)),+ input_text: (value) => claimed(normalizeTextPart(value)),+ output_text: (value) => claimed(normalizeTextPart(value)),+ reasoning: (value) => claimed(normalizeReasoningTextPart(value)),+};++function createPartContext(parserContext?: ParserContext): PartHandlerContext {+ return {+ normalizePart: (value) => normalizePart(value, parserContext),+ normalizePartList: (values) => normalizePartList(values, parserContext),+ };+}++/** Value-array -> parts mapper handed to providers as `context.normalizePartList`. */+export function normalizePartList(+ values: unknown[],+ parserContext?: ParserContext,+): NormalizedMessagePart[] {+ const parts: NormalizedMessagePart[] = [];+ for (const value of values) {+ if (typeof value === "string") {+ parts.push(...normalizeMediaPartsFromString(value));+ continue;+ }++ const part = normalizePart(value, parserContext);+ if (!part) continue;++ // Text parts frequently embed media reference tokens mid-string; split+ // them out. Refusals and annotated text stay intact.+ if (part.type === "text" && !part.providerMetadata && !part.refusal) {+ parts.push(...normalizeMediaPartsFromString(part.text));+ continue;+ }+ parts.push(part);+ }+ return parts;+}++function normalizePartBase(+ value: unknown,+ parserContext?: ParserContext,+): NormalizedMessagePart | null {+ if (typeof value === "string") {+ const mediaReference = parseMediaReference(value);+ if (mediaReference) return filePartFromMediaReference(mediaReference);+ return { type: "text", text: value };+ }+ if (!isRecord(value)) return null;++ const partContext = createPartContext(parserContext);+ const providers = providersInOrder(parserContext?.preferredProvider);++ const type = typeof value.type === "string" ? value.type : undefined;+ if (type) {+ const sharedHandler = ownLookup(SHARED_TYPED_PART_HANDLERS, type);+ if (sharedHandler) {+ const result = sharedHandler(value, partContext);+ if (result.matched) return result.value;+ }++ for (const provider of providers) {+ const handler = ownLookup(provider.typedParts, type);+ if (!handler) continue;+ const result = handler(value, partContext);+ if (result.matched) return result.value;+ }+ } else {+ for (const provider of providers) {+ const result = provider.tryNormalizeUntypedPart?.(value, partContext);+ if (result?.matched) return result.value;+ }+ }++ return normalizeFallbackPart(value);+}++const COMMON_CONSUMED_PART_KEYS = [+ "type",+ "providerMetadata",+ "providerOptions",+] as const;++const CONSUMED_PART_KEYS_BY_TYPE: Record<+ NormalizedMessagePart["type"],+ ReadonlySet<string>+> = {+ text: new Set([+ ...COMMON_CONSUMED_PART_KEYS,+ "text",+ "content",+ "thought",+ "thoughtSignature",+ "thought_signature",+ "refusal",+ "citations",+ "annotations",+ ]),+ reasoning: new Set([+ ...COMMON_CONSUMED_PART_KEYS,+ "text",+ "content",+ "thinking",+ "signature",+ "data",+ "encrypted_content",+ "summary",+ "thought",+ "thoughtSignature",+ "thought_signature",+ ]),+ "tool-call": new Set([+ ...COMMON_CONSUMED_PART_KEYS,+ "id",+ "call_id",+ "toolCallId",+ "name",+ "toolName",+ "input",+ "arguments",+ "args",+ "function",+ "function_call",+ "functionCall",+ "custom",+ "executable_code",+ "executableCode",+ "providerExecuted",+ ]),+ "tool-result": new Set([+ ...COMMON_CONSUMED_PART_KEYS,+ "id",+ "call_id",+ "toolCallId",+ "tool_use_id",+ "name",+ "toolName",+ "tool_name",+ "output",+ "response",+ "result",+ "content",+ "value",+ "error",+ "is_error",+ "isError",+ "function_response",+ "functionResponse",+ "code_execution_result",+ "codeExecutionResult",+ ]),+ file: new Set([+ ...COMMON_CONSUMED_PART_KEYS,+ "image",+ "data",+ "url",+ "mediaType",+ "mimeType",+ "mime_type",+ "filename",+ "file",+ "file_id",+ "file_data",+ "file_url",+ "image_url",+ "input_image",+ "input_audio",+ "inline_data",+ "inlineData",+ "fileData",+ "source",+ "format",+ ]),+ // These fallback parts already preserve the complete raw value. Inferring a+ // remainder would duplicate it, but explicit providerOptions/metadata still+ // merge below for consistency with recognized parts.+ data: new Set(),+ custom: new Set(),+};++/**+ * Known part fields normalize into the canonical union; every unconsumed raw+ * field becomes providerMetadata. AI SDK providerOptions and explicit/parser-+ * computed metadata win over the inferred remainder.+ */+function withProviderMetadata<T extends NormalizedMessagePart>(+ part: T,+ value: Record<string, unknown>,+): T {+ const providerOptions = asRecord(value.providerOptions);+ const explicitProviderMetadata = asRecord(value.providerMetadata);+ const explicitMetadata = {+ ...providerOptions,+ ...explicitProviderMetadata,+ ...part.providerMetadata,+ };+ const inferRemainder = part.type !== "data" && part.type !== "custom";+ const consumedKeys = new Set(CONSUMED_PART_KEYS_BY_TYPE[part.type]);++ // Provider-executed built-ins commonly turn every remaining raw payload+ // field (action, queries, code, results, ...) into their canonical input.+ // Exclude those dynamic keys so the same payload is not duplicated in+ // providerMetadata.+ if (+ part.type === "tool-call" &&+ part.providerExecuted === true &&+ isRecord(part.input)+ ) {+ for (const key of Object.keys(part.input)) consumedKeys.add(key);+ }++ const providerMetadata = remainingProviderMetadata(+ inferRemainder ? [value] : [],+ consumedKeys,+ explicitMetadata,+ );++ return providerMetadata ? ({ ...part, providerMetadata } as T) : part;+}++export function normalizePart(+ value: unknown,+ parserContext?: ParserContext,+): NormalizedMessagePart | null {+ const part = normalizePartBase(value, parserContext);+ if (!part) return null;+ const record = asRecord(value);+ const normalized = record ? withProviderMetadata(part, record) : part;++ if (normalized.type === "tool-call") {+ rawToolCallKeys.set(normalized, getRawToolCallKey(value, normalized));+ }++ return normalized;+}++/**+ * Preserve the legacy ingestion identity for idless calls while the raw+ * arguments are still available. The key is kept outside the public part+ * shape so client-safe consumers never see ingestion-only compatibility data.+ */+export function getToolCallKeyForPart(+ part: NormalizedMessagePart,+): string | undefined {+ return part.type === "tool-call" ? rawToolCallKeys.get(part) : undefined;+}++function getRawToolCallKey(+ value: unknown,+ part: Extract<NormalizedMessagePart, { type: "tool-call" }>,+): string {+ if (part.toolCallId) return `id:${part.toolCallId}`;++ const record = asRecord(value);+ const functionCall =+ asRecord(record?.function) ??+ asRecord(record?.functionCall) ??+ asRecord(record?.function_call);+ const rawArguments =+ functionCall?.arguments ??+ functionCall?.args ??+ functionCall?.input ??+ record?.arguments ??+ record?.args ??+ record?.input;+ let argumentsValue: string;+ if (typeof rawArguments === "string") {+ argumentsValue = rawArguments;+ } else {+ try {+ argumentsValue = JSON.stringify(rawArguments ?? {});+ } catch {+ argumentsValue = JSON.stringify(part.input);+ }+ }++ return `value:${part.toolName}:${argumentsValue}`;+}diff --git a/packages/shared/src/utils/normalized-io/core/normalize/role.ts b/packages/shared/src/utils/normalized-io/core/normalize/role.tsnew file mode 100644index 0000000000..9053ec00e9--- /dev/null+++ b/packages/shared/src/utils/normalized-io/core/normalize/role.ts@@ -0,0 +1,69 @@+import { registeredProviders } from "../../conventions";+import type { NormalizedMessagePart, NormalizedMessageRole } from "../../types";+import { ownLookup } from "../utils/json";+const CANONICAL_ROLES = new Set(["system", "user", "assistant", "tool"]);++export function normalizeRole(+ message: Record<string, unknown>,+): NormalizedMessageRole | undefined {+ const rawRole = message.role ?? message.author; // author: gemini/PaLM-era+ if (typeof rawRole === "string") {+ const lowered = rawRole.toLowerCase();+ for (const provider of registeredProviders) {+ const role = ownLookup(provider.roleByRawRole, lowered);+ if (role) return role;+ }+ if (CANONICAL_ROLES.has(lowered)) return lowered as NormalizedMessageRole;+ // Unrecognized strings are identities, not roles (e.g. LangGraph agent+ // names): the caller falls back to the contextual role and preserves the+ // raw string as `senderName`.+ return undefined;+ }++ if (typeof message.type !== "string") return undefined;+ for (const provider of registeredProviders) {+ const role = ownLookup(provider.roleByMessageType, message.type);+ if (role) return role;+ }+ return undefined;+}++/**+ * Role corrections that depend on the collected parts: user turns holding+ * only tool results are tool turns; tool-labeled turns without a+ * tool_call_id (koog/Traceloop-style conversions) are either mislabeled+ * assistant call batches or tool responses (the content IS the result).+ */+export function coerceRole(+ role: NormalizedMessageRole,+ value: Record<string, unknown>,+ parts: NormalizedMessagePart[],+): NormalizedMessageRole {+ if (+ role === "user" &&+ parts.length > 0 &&+ parts.every((part) => part.type === "tool-result")+ ) {+ return "tool";+ }++ if (role === "tool" && !value.tool_call_id && parts.length > 0) {+ // Explicit boolean return type: TS would otherwise infer a type+ // predicate and narrow `parts` to TextPart[], rejecting the splice.+ if (parts.every((part): boolean => part.type === "tool-call")) {+ return "assistant";+ }+ if (parts.every((part): boolean => part.type === "text")) {+ const output = parts+ .map((part) => (part.type === "text" ? part.text : ""))+ .join("\n");+ parts.splice(0, parts.length, {+ type: "tool-result",+ toolCallId: null,+ output,+ });+ }+ }++ return role;+}diff --git a/packages/shared/src/utils/normalized-io/core/normalize/tool-definitions.ts b/packages/shared/src/utils/normalized-io/core/normalize/tool-definitions.tsnew file mode 100644index 0000000000..e6c7a7e798--- /dev/null+++ b/packages/shared/src/utils/normalized-io/core/normalize/tool-definitions.ts@@ -0,0 +1,164 @@+import { registeredProviders } from "../../conventions";+import {+ asRecord,+ compact,+ isRecord,+ parseIfString,+ parseRecord,+ remainingProviderMetadata,+ toJsonValue,+} from "../utils/json";+import type { JsonObject, ToolDefinition } from "../../types";++// Keys consumed as definition fields by SOME dialect's extraction; everything+// else on a definition record is provider trivia lifted verbatim into+// providerMetadata. A union by necessity: metadata inference must know every+// dialect's consumed keys regardless of which extractor claimed the item.+const CONSUMED_DEFINITION_KEYS = new Set([+ "name",+ "description",+ "desc", // loose/traceloop+ "parameters", // openai, gemini+ "parameters_json_schema", // pydantic ai+ "input_schema", // anthropic+ "inputSchema", // ai sdk / mcp+ "format", // openai custom tools+ "function", // openai function-tool wrapper+ "custom", // openai custom-tool wrapper+ "type",+ "providerMetadata",+]);++/** Provider trivia beside the consumed definition fields, wrapper and inner+ * definition merged, explicit `providerMetadata` winning on collisions. */+export function toolDefinitionProviderMetadata(+ wrapper: Record<string, unknown>,+ definition: Record<string, unknown>,+): JsonObject | undefined {+ const explicit = asRecord(wrapper.providerMetadata);+ return remainingProviderMetadata(+ [wrapper, definition],+ CONSUMED_DEFINITION_KEYS,+ explicit,+ );+}++/**+ * Tool-definition normalization. Carrier mechanics (array / single record /+ * name-keyed map, JSON-string boundaries) live here; per-item recognition+ * folds the registry's `tryNormalizeToolDefinition`, with a loose fallback+ * for bare items no dialect claims. Pure: the accumulator collector owns+ * accumulation/merge.+ */++type ToolDefinitionOptions = {+ allowProviderToolWithoutName?: boolean;+ allowToolMap?: boolean;+};++/**+ * Loose fallback: bare `{name, description | desc, parameters}` items+ * (`parameters` is shared openai/gemini vocabulary) and β with the+ * carrier's allowance β nameless provider built-ins, named by their+ * `id`/`type` (openai `web_search_preview`, anthropic server tools, gemini+ * `googleSearch`-style groups).+ */+function normalizeLooseToolDefinition(+ value: Record<string, unknown>,+ options: ToolDefinitionOptions,+): ToolDefinition | null {+ const rawName =+ value.name ??+ (options.allowProviderToolWithoutName+ ? (value.id ?? (value.type !== "function" ? value.type : undefined))+ : undefined);++ return toolDefinition({+ name: rawName,+ description: value.description ?? value.desc /* loose/traceloop */,+ inputSchema: value.parameters /* openai, gemini */,+ type: value.type,+ providerMetadata: toolDefinitionProviderMetadata(value, value),+ });+}++function normalizeDefinitionItem(+ value: unknown,+ options: ToolDefinitionOptions,+): ToolDefinition | null {+ if (!isRecord(value)) return null;++ for (const provider of registeredProviders) {+ const result = provider.tryNormalizeToolDefinition?.(value);+ if (result?.matched) return result.value;+ }++ return normalizeLooseToolDefinition(value, options);+}++/**+ * Normalizes one tool-definition carrier value: an array, a single+ * definition record, or (with `allowToolMap`) a map keyed by tool name.+ */+export function normalizeToolDefinitionValue(+ value: unknown,+ options: ToolDefinitionOptions = {},+): ToolDefinition[] {+ const parsed = parseIfString(value);++ if (Array.isArray(parsed)) {+ return parsed+ .map((item) => normalizeDefinitionItem(parseIfString(item), options))+ .filter((def): def is ToolDefinition => def !== null);+ }++ if (!isRecord(parsed)) return [];++ const singleDefinition = normalizeDefinitionItem(parsed, options);+ if (singleDefinition) return [singleDefinition];++ if (!options.allowToolMap) return [];++ // Some instrumentation exports definitions as a map keyed by tool name.+ const definitions: ToolDefinition[] = [];+ for (const [name, rawDefinition] of Object.entries(parsed)) {+ const definition = parseRecord(rawDefinition);+ if (!definition) continue;++ const normalized = normalizeDefinitionItem({ name, ...definition }, {});+ if (normalized) definitions.push(normalized);+ }+ return definitions;+}++export type ToolDefinitionFields = {+ /** Must be a non-empty string, otherwise no definition is constructed. */+ name: unknown;+ /** Kept only when a string. */+ description?: unknown;+ /** Raw schema payload; one JSON-string boundary is parsed. */+ inputSchema?: unknown;+ /** Kept only when a string. */+ type?: unknown;+ providerMetadata?: JsonObject;+};++/** Canonical tool-definition constructor β same contract as the tool-part+ * builders: callers extract their own dialect's fields. */+export function toolDefinition(+ fields: ToolDefinitionFields,+): ToolDefinition | null {+ if (typeof fields.name !== "string" || fields.name.length === 0) return null;++ return compact<ToolDefinition>({+ name: fields.name,+ description:+ typeof fields.description === "string" ? fields.description : undefined,+ inputSchema:+ fields.inputSchema === undefined+ ? undefined+ : toJsonValue(parseIfString(fields.inputSchema)),+ type: typeof fields.type === "string" ? fields.type : undefined,+ providerMetadata: fields.providerMetadata,+ });+}diff --git a/packages/shared/src/utils/normalized-io/core/parser-context.ts b/packages/shared/src/utils/normalized-io/core/parser-context.tsnew file mode 100644index 0000000000..96000531ec--- /dev/null+++ b/packages/shared/src/utils/normalized-io/core/parser-context.ts@@ -0,0 +1,24 @@+import type { IOConvention } from "../conventions/io-convention";++/**+ * Mutable state for parsing one observation side. Input is conversation+ * history and therefore keeps every tool call; output is the side projected+ * into the legacy tool columns and deduplicates repeated representations.+ */+export type ParserContext =+ | {+ source: "input";+ hasSystemMessage: boolean;+ preferredProvider?: IOConvention;+ }+ | {+ source: "output";+ toolCallKeys: Set<string>;+ preferredProvider?: IOConvention;+ };++export function createParserContext(source: "input" | "output"): ParserContext {+ return source === "input"+ ? { source, hasSystemMessage: false }+ : { source, toolCallKeys: new Set<string>() };+}diff --git a/packages/shared/src/utils/normalized-io/core/utils/format.ts b/packages/shared/src/utils/normalized-io/core/utils/format.tsnew file mode 100644index 0000000000..e26c374bc9--- /dev/null+++ b/packages/shared/src/utils/normalized-io/core/utils/format.ts@@ -0,0 +1,22 @@+import { registeredProviders } from "../../conventions";++/** `role`/`content` are the universal message keys; every other container+ * key is provider vocabulary contributed via `messageLikeKeys`. */+export function isMessageLike(value: Record<string, unknown>): boolean {+ if ("role" in value || "content" in value) return true;+ return registeredProviders.some((provider) => {+ for (const key of provider.messageLikeKeys ?? []) {+ if (key in value) return true;+ }+ return false;+ });+}++/** Tool-definition messages fold: shapes only a convention can recognize. */+export function isToolDefinitionMessage(+ message: Record<string, unknown>,+): boolean {+ return registeredProviders.some(+ (provider) => provider.isToolDefinitionMessage?.(message) ?? false,+ );+}diff --git a/packages/shared/src/utils/normalized-io/core/utils/json.ts b/packages/shared/src/utils/normalized-io/core/utils/json.tsnew file mode 100644index 0000000000..ee30fc156f--- /dev/null+++ b/packages/shared/src/utils/normalized-io/core/utils/json.ts@@ -0,0 +1,130 @@+import type { JsonObject, JsonValue } from "../../types";++/**+ * Generic JSON/string utilities shared by the parser and the convention+ * modules. No provider knowledge lives here.+ */++export function isRecord(value: unknown): value is Record<string, unknown> {+ return typeof value === "object" && value !== null && !Array.isArray(value);+}++/**+ * Own-property lookup for input-derived keys: `__proto__`/`constructor` must+ * miss (taking the caller's unknown-value fallback), not resolve through the+ * prototype chain to non-handler values.+ */+export function ownLookup<T>(+ record: Readonly<Record<string, T>> | undefined,+ key: string,+): T | undefined {+ return record !== undefined && Object.hasOwn(record, key)+ ? record[key]+ : undefined;+}++export function toJsonValue(value: unknown): JsonValue {+ if (value === null) return null;+ if (typeof value === "string" || typeof value === "boolean") return value;+ if (typeof value === "number") {+ return Number.isFinite(value) ? value : String(value);+ }+ if (typeof value === "bigint") return value.toString();+ if (Array.isArray(value)) return value.map(toJsonValue);+ if (isRecord(value)) {+ return Object.fromEntries(+ Object.entries(value).map(([key, nestedValue]) => [+ key,+ toJsonValue(nestedValue),+ ]),+ );+ }++ // Telemetry values should already be JSON-compatible. Preserve an explicit+ // fallback instead of dropping an unexpected value from the custom part.+ return value === undefined ? null : String(value);+}++export function optionalString(value: unknown): string | undefined {+ return typeof value === "string" && value.length > 0 ? value : undefined;+}++export function nullableString(value: unknown): string | null {+ return typeof value === "string" && value.length > 0 ? value : null;+}++/** Parse one JSON-string boundary. Nested values are parsed only by their owner. */+export function parseIfString(value: unknown): unknown {+ if (typeof value !== "string") return value;++ try {+ return JSON.parse(value);+ } catch {+ return value;+ }+}++export function asRecord(value: unknown): Record<string, unknown> | undefined {+ return isRecord(value) ? value : undefined;+}++export function parseRecord(+ value: unknown,+): Record<string, unknown> | undefined {+ return asRecord(parseIfString(value));+}++export function parseArray(value: unknown): unknown[] | undefined {+ const parsed = parseIfString(value);+ return Array.isArray(parsed) ? parsed : undefined;+}++export function toProviderMetadata(+ entries: Record<string, unknown>,+): JsonObject | undefined {+ const value = toJsonValue(entries);+ return isRecord(value) && Object.keys(value).length > 0 ? value : undefined;+}++/**+ * Preserve the fields a normalizer did not consume. Records are folded in+ * order and explicit metadata wins last, matching object-spread semantics+ * without requiring every provider handler to rebuild the same remainder.+ */+export function remainingProviderMetadata(+ records: readonly Record<string, unknown>[],+ consumedKeys: ReadonlySet<string>,+ explicitMetadata?: Record<string, unknown>,+): JsonObject | undefined {+ const remaining: Record<string, unknown> = {};++ for (const record of records) {+ for (const [key, value] of Object.entries(record)) {+ if (!consumedKeys.has(key)) remaining[key] = value;+ }+ }++ if (explicitMetadata) {+ for (const [key, value] of Object.entries(explicitMetadata)) {+ remaining[key] = value;+ }+ }++ return toProviderMetadata(remaining);+}++/** Strip undefined-valued keys so optional fields are absent, not undefined. */+export function compact<T extends Record<string, unknown>>(obj: T): T {+ return Object.fromEntries(+ Object.entries(obj).filter(([, value]) => value !== undefined),+ ) as T;+}++export function omitKeys(+ record: Record<string, unknown>,+ keys: string[],+): Record<string, unknown> {+ return Object.fromEntries(+ Object.entries(record).filter(([key]) => !keys.includes(key)),+ );+}diff --git a/packages/shared/src/utils/normalized-io/core/utils/providers.ts b/packages/shared/src/utils/normalized-io/core/utils/providers.tsnew file mode 100644index 0000000000..076e744e4f--- /dev/null+++ b/packages/shared/src/utils/normalized-io/core/utils/providers.ts@@ -0,0 +1,13 @@+import { registeredProviders } from "../../conventions";+import type { IOConvention } from "../../conventions/io-convention";++/** Put a claimed root provider first without excluding other dialects. */+export function providersInOrder(+ preferredProvider?: IOConvention,+): readonly IOConvention[] {+ if (!preferredProvider) return registeredProviders;+ return [+ preferredProvider,+ ...registeredProviders.filter((provider) => provider !== preferredProvider),+ ];+}diff --git a/packages/shared/src/utils/normalized-io/index.ts b/packages/shared/src/utils/normalized-io/index.tsnew file mode 100644index 0000000000..d41d85e3e4--- /dev/null+++ b/packages/shared/src/utils/normalized-io/index.ts@@ -0,0 +1,2 @@+export { normalizeSpanIO } from "./parser";+export type * from "./types";diff --git a/packages/shared/src/utils/normalized-io/parser.ts b/packages/shared/src/utils/normalized-io/parser.tsnew file mode 100644index 0000000000..06bbf7ce12--- /dev/null+++ b/packages/shared/src/utils/normalized-io/parser.ts@@ -0,0 +1,59 @@+import { asRecord, parseArray, parseIfString } from "./core/utils/json";+import type { NormalizedIO, SpanIO } from "./types";+import {+ collectMetadata,+ collectIO,+ createAccumulator,+ type ParsedIOValue,+} from "./core/accumulator";+import { createParserContext } from "./core/parser-context";++type ParsedSpanIO = {+ input: ParsedIOValue;+ output: ParsedIOValue;+ metadata: ParsedIOValue;+};++function parseIOValue(value: unknown): ParsedIOValue {+ const parsed = parseIfString(value);+ const record = asRecord(parsed);++ return {+ value: parsed,+ record,+ messages: Array.isArray(parsed)+ ? parsed+ : record+ ? parseArray(record.messages)+ : undefined,+ };+}++function parseSpanIO(span: SpanIO): ParsedSpanIO {+ return {+ input: parseIOValue(span.input),+ output: parseIOValue(span.output),+ metadata: parseIOValue(span.metadata),+ };+}++/**+ * Normalize an already-adapted span I/O value.+ *+ * This is the client-safe parser entry point. Source-specific adapters such as+ * ClickHouse event rows and OTel spans stay in the server-only wrapper.+ */+export function normalizeSpanIO(span: SpanIO): NormalizedIO {+ const { input, output, metadata } = parseSpanIO(span);+ const accumulator = createAccumulator();++ collectIO(input, createParserContext("input"), accumulator);+ collectIO(output, createParserContext("output"), accumulator);+ collectMetadata(metadata, accumulator);++ return {+ messages: accumulator.messages,+ toolDefinitions: accumulator.toolDefinitions,+ span,+ };+}diff --git a/packages/shared/src/utils/normalized-io/types.ts b/packages/shared/src/utils/normalized-io/types.tsnew file mode 100644index 0000000000..cc88b49fff--- /dev/null+++ b/packages/shared/src/utils/normalized-io/types.ts@@ -0,0 +1,198 @@+/**+ * The transport-independent observation I/O boundary.+ *+ * OTel and legacy ingestion adapters should produce this shape before a+ * canonical normalized-I/O parser is applied. The values intentionally remain+ * unknown here: parsing and normalization are separate steps.+ */+export type SpanIO = {+ input: unknown;+ output: unknown;+ metadata: unknown;+};++export type JsonPrimitive = string | number | boolean | null;++export type JsonObject = { [key: string]: JsonValue };++export type JsonValue = JsonPrimitive | JsonObject | JsonValue[];++/**+ * Semantics the parser computes are typed fields on their parts (e.g.+ * `refusal`, `reasoning`, `invalid`); `providerMetadata` preserves every raw+ * provider field that the canonical part did not consume, plus parser-derived+ * provider payloads such as unified citations.+ * Part types encode what a consumer can do with a part; new provider+ * concepts start as providerMetadata and get promoted to a typed field once+ * they are cross-provider semantics we compute and consumers filter on.+ */+export type NormalizedPartBase = {+ providerMetadata?: JsonObject;+};++export type TextPart = NormalizedPartBase & {+ type: "text";+ /** Model refusal text (OpenAI refusal parts / `message.refusal`) β stays+ * in the conversation stream, filterable for evals. */+ refusal?: true;+ text: string;+};++/**+ * One reasoning part type with a discriminated payload instead of+ * per-provider part types (mirrors FilePart.content):+ * - `text` β visible chain-of-thought or summaries; Anthropic's integrity+ * `signature` rides on it since it certifies exactly that text.+ * - `redacted` β a blob the provider withheld (Anthropic redacted_thinking).+ * - `encrypted` β a blob the provider returns for replay (OpenAI Responses+ * encrypted_content).+ * - `data` β structured/unrecognized reasoning payloads.+ * Opaque blobs are first-class stream elements rather than providerMetadata+ * piggybacks so replay and audit consumers can find them.+ */+export type ReasoningPart = NormalizedPartBase & {+ type: "reasoning";+ content:+ | { kind: "text"; text: string; signature?: string }+ | { kind: "redacted"; data: string }+ | { kind: "encrypted"; data: string }+ | { kind: "data"; value: JsonValue };+};++export type ToolCallPart = NormalizedPartBase & {+ type: "tool-call";+ toolCallId: string | null;+ toolName: string;+ input: JsonValue;+ /** Raw source type used by compatibility projections (for example+ * `function`, `custom`, or a provider-specific built-in kind). */+ toolType?: string;+ providerExecuted?: boolean;+ /** Attempt the model made whose arguments could not be parsed (e.g.+ * LangChain invalid_tool_calls). Visible in the stream, filterable for+ * evals; excluded from the tool columns, which count executable calls. */+ invalid?: true;+};++export type ToolResultPart = NormalizedPartBase & {+ type: "tool-result";+ toolCallId: string | null;+ toolName?: string;+ output: JsonValue;+ isError?: boolean;+};++export type FilePart = NormalizedPartBase & {+ type: "file";+ /**+ * IANA media type when the source declares one (data-URI prefix, Langfuse+ * media reference token, explicit format field). Modality wildcards like+ * `image/*` / `audio/*` when only the part kind reveals the modality;+ * absent when the source gives no signal (e.g. an opaque file id).+ */+ mediaType?: string;+ filename?: string;+ /** File was produced as reasoning output (e.g. AI SDK reasoning-file). */+ reasoning?: true;+ content:+ | { kind: "url"; url: string }+ | { kind: "base64"; data: string }+ | { kind: "reference"; id: string };+};++export type DataPart = NormalizedPartBase & {+ type: "data";+ name?: string;+ value: JsonValue;+};++export type CustomPart = NormalizedPartBase & {+ type: "custom";+ kind: string;+ value: JsonValue;+};++export type NormalizedMessagePart =+ | TextPart+ | ReasoningPart+ | ToolCallPart+ | ToolResultPart+ | FilePart+ | DataPart+ | CustomPart;++export type NormalizedMessageRole = "system" | "user" | "assistant" | "tool";++/**+ * How the model's turn ended, canonicalized across providers so consumers+ * filter one vocabulary (e.g. "all length-truncated observations"). `raw`+ * keeps the provider's verbatim value, so canonicalization loses nothing+ * (e.g. { type: "stop", raw: "stop_sequence" } vs natural completion).+ */+export type FinishReason = {+ type:+ | "stop"+ | "length"+ | "tool-calls"+ | "content-filter"+ | "error"+ | "other"+ | "unknown";+ raw: string;+};++export type NormalizedMessage = {+ id?: string;+ role: NormalizedMessageRole;+ /**+ * Sender identity beyond the canonical role: an explicit participant name+ * (OpenAI/LangChain `name`, e.g. `alice` or `example_user`) or the raw+ * declared role when it is not a recognized role (e.g. multi-agent frameworks+ * putting the agent name in the role field)+ */+ senderName?: string;+ parts: NormalizedMessagePart[];+ // Turn metadata, not conversation content β a message field rather than a+ // part so text extraction, rendering, and tool-column paths stay untouched.+ finishReason?: FinishReason;+ // Preserves the observation input/output boundary in one ordered stream.+ source: "input" | "output";+};++export type ToolDefinition = {+ name: string;+ description?: string;+ inputSchema?: JsonValue;+ type?: string;+ /** Raw provider fields not consumed by the canonical definition. */+ providerMetadata?: JsonObject;+};++export type NormalizedIO = {+ // Input messages followed by output messages. Message and part order is preserved.+ messages: NormalizedMessage[];+ toolDefinitions: ToolDefinition[];+ /** Raw pre-normalization values compiled from the source; always returned for now. */+ span: SpanIO;+};++/** Decoded value stored as one JSON string in tool_definitions. */+export type ToolDefinitionColumnValue = {+ description: string;+ parameters: string;+};++/** Decoded value stored as one JSON string in tool_calls. */+export type ToolCallColumnValue = {+ id: string;+ arguments: string;+ type: string;+ index: number;+};++/** Exact serialized compatibility shape written to ClickHouse. */+export type ToolColumns = {+ tool_definitions: Record<string, string>;+ tool_calls: string[];+ tool_call_names: string[];+};