@assemble-dev/providers 0.3.0 → 0.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs CHANGED
@@ -72,7 +72,9 @@ __export(index_exports, {
72
72
  extractMessageText: () => extractMessageText,
73
73
  gemini: () => gemini,
74
74
  isAbortError: () => isAbortError,
75
+ isOpenAiReasoningModel: () => isOpenAiReasoningModel,
75
76
  isRetryableProviderAppError: () => isRetryableProviderAppError,
77
+ isZodV4Schema: () => isZodV4Schema,
76
78
  joinSystemMessageText: () => joinSystemMessageText,
77
79
  mapAnthropicResponseToChatCompletionResult: () => mapAnthropicResponseToChatCompletionResult,
78
80
  mapAnthropicUsageToTokenUsage: () => mapAnthropicUsageToTokenUsage,
@@ -95,15 +97,19 @@ __export(index_exports, {
95
97
  parseAnthropicToolInputJson: () => parseAnthropicToolInputJson,
96
98
  parseAzureOpenAiStreamEvents: () => parseAzureOpenAiStreamEvents,
97
99
  parseAzureOpenAiToolArgumentsJson: () => parseAzureOpenAiToolArgumentsJson,
100
+ prependStructuredOutputJsonOnlySystemMessage: () => prependStructuredOutputJsonOnlySystemMessage,
98
101
  resolveAnthropicPromptCachingSettings: () => resolveAnthropicPromptCachingSettings,
99
102
  resolveBedrockCredentials: () => resolveBedrockCredentials,
103
+ resolveOpenAiStructuredOutputMode: () => resolveOpenAiStructuredOutputMode,
100
104
  resolveProviderApiKey: () => resolveProviderApiKey,
101
105
  sanitizeJsonSchemaForAnthropicStructuredOutput: () => sanitizeJsonSchemaForAnthropicStructuredOutput,
102
106
  sanitizeJsonSchemaForGeminiResponseSchema: () => sanitizeJsonSchemaForGeminiResponseSchema,
103
107
  sanitizeJsonSchemaForOpenAiStructuredOutput: () => sanitizeJsonSchemaForOpenAiStructuredOutput,
104
108
  signAwsRequestWithSigV4: () => signAwsRequestWithSigV4,
105
109
  streamAnthropicChatCompletion: () => streamAnthropicChatCompletion,
106
- streamAzureOpenAiChatCompletion: () => streamAzureOpenAiChatCompletion
110
+ streamAzureOpenAiChatCompletion: () => streamAzureOpenAiChatCompletion,
111
+ structuredOutputJsonOnlySystemInstruction: () => structuredOutputJsonOnlySystemInstruction,
112
+ structuredOutputJsonOnlySystemMessage: () => structuredOutputJsonOnlySystemMessage
107
113
  });
108
114
  module.exports = __toCommonJS(index_exports);
109
115
 
@@ -313,6 +319,9 @@ var openAiUnsupportedSchemaKeywords = [
313
319
  "uniqueItems",
314
320
  "default"
315
321
  ];
322
+ function isZodV4Schema(schema) {
323
+ return "_zod" in schema;
324
+ }
316
325
  function convertZodSchemaToJsonSchema(schema) {
317
326
  try {
318
327
  return import_json2.JsonObjectSchema.parse(import_zod2.z.toJSONSchema(schema, { io: "input" }));
@@ -377,18 +386,72 @@ function listSchemaPropertyNames(node) {
377
386
  return Object.keys(properties);
378
387
  }
379
388
 
389
+ // src/structured-output-fallback.ts
390
+ var structuredOutputJsonOnlySystemInstruction = "Respond with a single JSON object that satisfies the caller's requested structure. Output only valid JSON with no surrounding text.";
391
+ var structuredOutputJsonOnlySystemMessage = {
392
+ role: "system",
393
+ content: structuredOutputJsonOnlySystemInstruction
394
+ };
395
+ function prependStructuredOutputJsonOnlySystemMessage(messages) {
396
+ return [structuredOutputJsonOnlySystemMessage, ...messages];
397
+ }
398
+
380
399
  // src/openai-request-mapping.ts
400
+ var openAiReasoningModelNamePrefixes = [
401
+ "gpt-5",
402
+ "o1",
403
+ "o3",
404
+ "o4"
405
+ ];
406
+ function isOpenAiReasoningModel(model) {
407
+ return openAiReasoningModelNamePrefixes.some(
408
+ (prefix) => matchesOpenAiModelNamePrefix(model, prefix)
409
+ );
410
+ }
411
+ function matchesOpenAiModelNamePrefix(model, prefix) {
412
+ if (!model.startsWith(prefix)) {
413
+ return false;
414
+ }
415
+ const boundaryCharacter = model.charAt(prefix.length);
416
+ return boundaryCharacter === "" || boundaryCharacter === "-" || boundaryCharacter === ".";
417
+ }
418
+ function resolveOpenAiStructuredOutputMode(messages, schema) {
419
+ if (isZodV4Schema(schema)) {
420
+ return {
421
+ messages,
422
+ structuredOutputJsonSchema: buildOpenAiStructuredOutputJsonSchema(schema)
423
+ };
424
+ }
425
+ return {
426
+ messages: prependStructuredOutputJsonOnlySystemMessage(messages),
427
+ requireJsonObjectResponse: true
428
+ };
429
+ }
381
430
  function buildOpenAiRequestBody(model, input) {
382
431
  const requestBody = {
383
432
  model,
384
433
  messages: input.messages.map(mapChatMessageToOpenAiRequestMessage)
385
434
  };
386
- if (input.temperature !== void 0) {
435
+ applyOpenAiGenerationSettings(requestBody, model, input);
436
+ applyOpenAiResponseFormat(requestBody, input);
437
+ applyOpenAiToolFields(requestBody, input);
438
+ return requestBody;
439
+ }
440
+ function applyOpenAiGenerationSettings(requestBody, model, input) {
441
+ const reasoningModel = input.reasoningModel ?? isOpenAiReasoningModel(model);
442
+ if (input.temperature !== void 0 && !reasoningModel) {
387
443
  requestBody.temperature = input.temperature;
388
444
  }
389
- if (input.maxOutputTokens !== void 0) {
390
- requestBody.max_tokens = input.maxOutputTokens;
445
+ if (input.maxOutputTokens === void 0) {
446
+ return;
447
+ }
448
+ if (reasoningModel) {
449
+ requestBody.max_completion_tokens = input.maxOutputTokens;
450
+ return;
391
451
  }
452
+ requestBody.max_tokens = input.maxOutputTokens;
453
+ }
454
+ function applyOpenAiResponseFormat(requestBody, input) {
392
455
  if (input.structuredOutputJsonSchema !== void 0) {
393
456
  requestBody.response_format = {
394
457
  type: "json_schema",
@@ -398,9 +461,13 @@ function buildOpenAiRequestBody(model, input) {
398
461
  schema: input.structuredOutputJsonSchema
399
462
  }
400
463
  };
401
- } else if (input.requireJsonObjectResponse === true) {
464
+ return;
465
+ }
466
+ if (input.requireJsonObjectResponse === true) {
402
467
  requestBody.response_format = { type: "json_object" };
403
468
  }
469
+ }
470
+ function applyOpenAiToolFields(requestBody, input) {
404
471
  if (input.tools !== void 0 && input.tools.length > 0) {
405
472
  requestBody.tools = input.tools.map(mapModelToolDefinitionToOpenAiTool);
406
473
  }
@@ -409,7 +476,6 @@ function buildOpenAiRequestBody(model, input) {
409
476
  input.toolChoice
410
477
  );
411
478
  }
412
- return requestBody;
413
479
  }
414
480
  function mapModelToolDefinitionToOpenAiTool(tool) {
415
481
  return {
@@ -770,12 +836,9 @@ function openai(options) {
770
836
  },
771
837
  async generateStructuredOutput(request) {
772
838
  const completion = await executeOpenAiAdapterCall(options, {
773
- messages: request.messages,
839
+ ...resolveOpenAiStructuredOutputMode(request.messages, request.schema),
774
840
  temperature: request.temperature ?? options.temperature,
775
841
  maxOutputTokens: request.maxOutputTokens ?? options.maxOutputTokens,
776
- structuredOutputJsonSchema: buildOpenAiStructuredOutputJsonSchema(
777
- request.schema
778
- ),
779
842
  abortSignal: request.abortSignal
780
843
  });
781
844
  return {
@@ -1543,15 +1606,10 @@ function anthropic(options) {
1543
1606
  );
1544
1607
  },
1545
1608
  async generateStructuredOutput(request) {
1546
- const completion = await executeAnthropicAdapterCall(options, {
1547
- messages: request.messages,
1548
- temperature: request.temperature ?? options.temperature,
1549
- maxOutputTokens: request.maxOutputTokens ?? options.maxOutputTokens,
1550
- structuredOutputJsonSchema: buildAnthropicStructuredOutputJsonSchema(
1551
- request.schema
1552
- ),
1553
- abortSignal: request.abortSignal
1554
- });
1609
+ const completion = await executeAnthropicAdapterCall(
1610
+ options,
1611
+ buildAnthropicStructuredOutputInput(options, request)
1612
+ );
1555
1613
  return {
1556
1614
  value: parseStructuredJsonText(completion.content, request.schema),
1557
1615
  tokenUsage: completion.tokenUsage,
@@ -1565,6 +1623,26 @@ function anthropic(options) {
1565
1623
  }
1566
1624
  };
1567
1625
  }
1626
+ function buildAnthropicStructuredOutputInput(options, request) {
1627
+ const baseInput = {
1628
+ messages: request.messages,
1629
+ temperature: request.temperature ?? options.temperature,
1630
+ maxOutputTokens: request.maxOutputTokens ?? options.maxOutputTokens,
1631
+ abortSignal: request.abortSignal
1632
+ };
1633
+ if (isZodV4Schema(request.schema)) {
1634
+ return {
1635
+ ...baseInput,
1636
+ structuredOutputJsonSchema: buildAnthropicStructuredOutputJsonSchema(
1637
+ request.schema
1638
+ )
1639
+ };
1640
+ }
1641
+ return {
1642
+ ...baseInput,
1643
+ messages: prependStructuredOutputJsonOnlySystemMessage(request.messages)
1644
+ };
1645
+ }
1568
1646
  function buildAnthropicCallInput(options, request) {
1569
1647
  return {
1570
1648
  messages: request.messages,
@@ -1789,13 +1867,10 @@ function gemini(options) {
1789
1867
  });
1790
1868
  },
1791
1869
  async generateStructuredOutput(request) {
1792
- const completion = await executeGeminiAdapterCall(options, {
1793
- messages: request.messages,
1794
- temperature: request.temperature ?? options.temperature,
1795
- maxOutputTokens: request.maxOutputTokens ?? options.maxOutputTokens,
1796
- responseSchema: buildGeminiResponseSchema(request.schema),
1797
- abortSignal: request.abortSignal
1798
- });
1870
+ const completion = await executeGeminiAdapterCall(
1871
+ options,
1872
+ buildGeminiStructuredOutputInput(options, request)
1873
+ );
1799
1874
  return {
1800
1875
  value: parseStructuredJsonText(completion.content, request.schema),
1801
1876
  tokenUsage: completion.tokenUsage,
@@ -1804,6 +1879,25 @@ function gemini(options) {
1804
1879
  }
1805
1880
  };
1806
1881
  }
1882
+ function buildGeminiStructuredOutputInput(options, request) {
1883
+ const baseInput = {
1884
+ messages: request.messages,
1885
+ temperature: request.temperature ?? options.temperature,
1886
+ maxOutputTokens: request.maxOutputTokens ?? options.maxOutputTokens,
1887
+ abortSignal: request.abortSignal
1888
+ };
1889
+ if (isZodV4Schema(request.schema)) {
1890
+ return {
1891
+ ...baseInput,
1892
+ responseSchema: buildGeminiResponseSchema(request.schema)
1893
+ };
1894
+ }
1895
+ return {
1896
+ ...baseInput,
1897
+ messages: prependStructuredOutputJsonOnlySystemMessage(request.messages),
1898
+ requireJsonResponse: true
1899
+ };
1900
+ }
1807
1901
  async function executeGeminiAdapterCall(options, input) {
1808
1902
  return await executeProviderCallWithModelFallback(
1809
1903
  options.fallbackModel,
@@ -1921,6 +2015,8 @@ function buildGeminiGenerationConfig(input) {
1921
2015
  if (input.responseSchema !== void 0) {
1922
2016
  generationConfig.responseMimeType = "application/json";
1923
2017
  generationConfig.responseSchema = input.responseSchema;
2018
+ } else if (input.requireJsonResponse === true) {
2019
+ generationConfig.responseMimeType = "application/json";
1924
2020
  }
1925
2021
  if (Object.keys(generationConfig).length === 0) {
1926
2022
  return void 0;
@@ -2128,7 +2224,7 @@ var import_errors29 = require("@assemble-dev/shared-utils/errors");
2128
2224
  var import_errors26 = require("@assemble-dev/shared-types/errors");
2129
2225
  var import_json7 = require("@assemble-dev/shared-types/json");
2130
2226
  var import_errors27 = require("@assemble-dev/shared-utils/errors");
2131
- var anthropicBatchJsonOnlySystemInstruction = "Respond with a single JSON object that satisfies the caller's requested structure. Output only valid JSON with no surrounding text.";
2227
+ var anthropicBatchJsonOnlySystemInstruction = structuredOutputJsonOnlySystemInstruction;
2132
2228
  function parseAnthropicBatchStructuredContent(content, schema) {
2133
2229
  const jsonValue = parseStructuredJsonText(
2134
2230
  content,
@@ -2935,6 +3031,7 @@ function azureOpenai(options) {
2935
3031
  messages: request.messages,
2936
3032
  temperature: request.temperature ?? options.temperature,
2937
3033
  maxOutputTokens: request.maxOutputTokens ?? options.maxOutputTokens,
3034
+ reasoningModel: options.reasoningModel,
2938
3035
  tools: request.tools,
2939
3036
  toolChoice: request.toolChoice,
2940
3037
  abortSignal: request.abortSignal,
@@ -2943,12 +3040,10 @@ function azureOpenai(options) {
2943
3040
  },
2944
3041
  async generateStructuredOutput(request) {
2945
3042
  const completion = await executeAzureOpenAiAdapterCall(options, {
2946
- messages: request.messages,
3043
+ ...resolveOpenAiStructuredOutputMode(request.messages, request.schema),
2947
3044
  temperature: request.temperature ?? options.temperature,
2948
3045
  maxOutputTokens: request.maxOutputTokens ?? options.maxOutputTokens,
2949
- structuredOutputJsonSchema: buildOpenAiStructuredOutputJsonSchema(
2950
- request.schema
2951
- ),
3046
+ reasoningModel: options.reasoningModel,
2952
3047
  abortSignal: request.abortSignal
2953
3048
  });
2954
3049
  return {
@@ -3047,6 +3142,7 @@ function buildAzureOpenAiStreamInput(options, request) {
3047
3142
  messages: request.messages,
3048
3143
  temperature: request.temperature ?? options.temperature,
3049
3144
  maxOutputTokens: request.maxOutputTokens ?? options.maxOutputTokens,
3145
+ reasoningModel: options.reasoningModel,
3050
3146
  tools: request.tools,
3051
3147
  toolChoice: request.toolChoice,
3052
3148
  abortSignal: request.abortSignal,
@@ -3341,6 +3437,9 @@ function buildBedrockInvokeModelRequestBody(input) {
3341
3437
  };
3342
3438
  }
3343
3439
  function buildBedrockStructuredOutputSystemMessage(schema) {
3440
+ if (!isZodV4Schema(schema)) {
3441
+ return structuredOutputJsonOnlySystemMessage;
3442
+ }
3344
3443
  return {
3345
3444
  role: "system",
3346
3445
  content: `Respond with a single JSON object that satisfies the following JSON Schema. Output only valid JSON with no surrounding text.
@@ -3403,7 +3502,9 @@ ${JSON.stringify(convertZodSchemaToJsonSchema(schema))}`
3403
3502
  extractMessageText,
3404
3503
  gemini,
3405
3504
  isAbortError,
3505
+ isOpenAiReasoningModel,
3406
3506
  isRetryableProviderAppError,
3507
+ isZodV4Schema,
3407
3508
  joinSystemMessageText,
3408
3509
  mapAnthropicResponseToChatCompletionResult,
3409
3510
  mapAnthropicUsageToTokenUsage,
@@ -3426,13 +3527,17 @@ ${JSON.stringify(convertZodSchemaToJsonSchema(schema))}`
3426
3527
  parseAnthropicToolInputJson,
3427
3528
  parseAzureOpenAiStreamEvents,
3428
3529
  parseAzureOpenAiToolArgumentsJson,
3530
+ prependStructuredOutputJsonOnlySystemMessage,
3429
3531
  resolveAnthropicPromptCachingSettings,
3430
3532
  resolveBedrockCredentials,
3533
+ resolveOpenAiStructuredOutputMode,
3431
3534
  resolveProviderApiKey,
3432
3535
  sanitizeJsonSchemaForAnthropicStructuredOutput,
3433
3536
  sanitizeJsonSchemaForGeminiResponseSchema,
3434
3537
  sanitizeJsonSchemaForOpenAiStructuredOutput,
3435
3538
  signAwsRequestWithSigV4,
3436
3539
  streamAnthropicChatCompletion,
3437
- streamAzureOpenAiChatCompletion
3540
+ streamAzureOpenAiChatCompletion,
3541
+ structuredOutputJsonOnlySystemInstruction,
3542
+ structuredOutputJsonOnlySystemMessage
3438
3543
  });
package/dist/index.d.cts CHANGED
@@ -357,12 +357,17 @@ declare function buildProviderCanceledError(providerLabel: string): AppError;
357
357
  declare function executeAbortableProviderFetch(providerLabel: string, performFetch: () => Promise<Response>): Promise<Response>;
358
358
 
359
359
  declare const openAiStructuredOutputSchemaName = "structured_output";
360
+ declare function isZodV4Schema<T>(schema: z.ZodType<T>): boolean;
360
361
  declare function convertZodSchemaToJsonSchema<T>(schema: z.ZodType<T>): JsonObject;
361
362
  declare function buildAnthropicStructuredOutputJsonSchema<T>(schema: z.ZodType<T>): JsonObject;
362
363
  declare function sanitizeJsonSchemaForAnthropicStructuredOutput(jsonSchema: JsonObject): JsonObject;
363
364
  declare function buildOpenAiStructuredOutputJsonSchema<T>(schema: z.ZodType<T>): JsonObject;
364
365
  declare function sanitizeJsonSchemaForOpenAiStructuredOutput(jsonSchema: JsonObject): JsonObject;
365
366
 
367
+ declare const structuredOutputJsonOnlySystemInstruction = "Respond with a single JSON object that satisfies the caller's requested structure. Output only valid JSON with no surrounding text.";
368
+ declare const structuredOutputJsonOnlySystemMessage: ChatMessage;
369
+ declare function prependStructuredOutputJsonOnlySystemMessage(messages: ChatMessage[]): ChatMessage[];
370
+
366
371
  declare function buildGeminiResponseSchema<T>(schema: z.ZodType<T>): JsonObject;
367
372
  declare function sanitizeJsonSchemaForGeminiResponseSchema(jsonSchema: JsonObject): JsonObject;
368
373
 
@@ -539,6 +544,7 @@ type OpenAiRequestBody = {
539
544
  messages: OpenAiRequestMessage[];
540
545
  temperature?: number;
541
546
  max_tokens?: number;
547
+ max_completion_tokens?: number;
542
548
  response_format?: OpenAiResponseFormat;
543
549
  tools?: OpenAiToolDefinition[];
544
550
  tool_choice?: OpenAiToolChoice;
@@ -547,6 +553,7 @@ type OpenAiCallInput = {
547
553
  messages: ChatMessage[];
548
554
  temperature?: number;
549
555
  maxOutputTokens?: number;
556
+ reasoningModel?: boolean;
550
557
  structuredOutputJsonSchema?: JsonObject;
551
558
  requireJsonObjectResponse?: boolean;
552
559
  tools?: ModelToolDefinition[];
@@ -554,6 +561,13 @@ type OpenAiCallInput = {
554
561
  abortSignal?: AbortSignal;
555
562
  extraBody?: Record<string, JsonValue>;
556
563
  };
564
+ type OpenAiStructuredOutputMode = {
565
+ messages: ChatMessage[];
566
+ structuredOutputJsonSchema?: JsonObject;
567
+ requireJsonObjectResponse?: boolean;
568
+ };
569
+ declare function isOpenAiReasoningModel(model: string): boolean;
570
+ declare function resolveOpenAiStructuredOutputMode<T>(messages: ChatMessage[], schema: z.ZodType<T>): OpenAiStructuredOutputMode;
557
571
  declare function buildOpenAiRequestBody(model: string, input: OpenAiCallInput): OpenAiRequestBody;
558
572
  declare function mapModelToolDefinitionToOpenAiTool(tool: ModelToolDefinition): OpenAiToolDefinition;
559
573
  declare function mapModelToolChoiceToOpenAiToolChoice(toolChoice: ModelToolChoice): OpenAiToolChoice;
@@ -695,6 +709,7 @@ type AzureOpenAiAdapterOptions = {
695
709
  fetchImplementation?: typeof fetch;
696
710
  temperature?: number;
697
711
  maxOutputTokens?: number;
712
+ reasoningModel?: boolean;
698
713
  retry?: ProviderRetryOptions;
699
714
  extraBody?: Record<string, JsonValue>;
700
715
  };
@@ -791,4 +806,4 @@ declare function buildBedrockInvokeModelUrl(region: string, modelId: string): st
791
806
  declare function resolveBedrockCredentials(options: BedrockAdapterOptions): BedrockCredentials;
792
807
  declare function buildBedrockInvokeModelRequestBody(input: AnthropicRequestInput): BedrockInvokeModelRequestBody;
793
808
 
794
- export { type AnthropicAdapterOptions, type AnthropicBatchApiRequest, type AnthropicBatchApiRequestMessage, type AnthropicBatchApiRequestParams, type AnthropicBatchCanceledOutcome, type AnthropicBatchClient, type AnthropicBatchClientOptions, type AnthropicBatchErroredOutcome, type AnthropicBatchExpiredOutcome, type AnthropicBatchMessageRequest, type AnthropicBatchPromptCachingMode, type AnthropicBatchResult, type AnthropicBatchResultOutcome, type AnthropicBatchSubmittedRequestSummary, type AnthropicBatchSucceededOutcome, type AnthropicBatchSystemContentBlock, type AnthropicBatchTraceEventHandler, type AnthropicBatchWaitOptions, type AnthropicCacheControl, type AnthropicCacheTtl, type AnthropicMessageBatch, type AnthropicMessageBatchProcessingStatus, type AnthropicMessageBatchRequestCounts, type AnthropicMessagesResponse, AnthropicMessagesResponseSchema, type AnthropicOutputConfig, type AnthropicPromptCachingMode, type AnthropicPromptCachingOptions, type AnthropicPromptCachingSettings, type AnthropicRequestBody, type AnthropicRequestContentBlock, type AnthropicRequestInput, type AnthropicRequestMessage, type AnthropicRequestSettings, type AnthropicStreamEvent, AnthropicStreamEventSchema, type AnthropicStreamRequestInput, type AnthropicStreamStartUsage, type AnthropicSystemContentBlock, type AnthropicThinkingOptions, type AnthropicToolChoice, type AnthropicToolDefinition, type AnthropicUsage, type AwsCredentials, type AwsSigV4SigningInput, type AzureOpenAiAdapterOptions, type AzureOpenAiChatCompletionResponse, AzureOpenAiChatCompletionResponseSchema, type AzureOpenAiChatRequestBody, type AzureOpenAiStreamRequestBody, type AzureOpenAiStreamRequestInput, type AzureOpenAiUsage, type BedrockAdapterOptions, type BedrockCredentials, type BedrockInvokeModelRequestBody, type ChatCompletionRequest, type ChatCompletionResult, type ChatMessage, type ChatMessageCacheControl, ChatMessageCacheControlSchema, type ChatMessageRole, ChatMessageRoleSchema, ChatMessageSchema, type ChatStopReason, ChatStopReasonSchema, type ChatStreamEvent, type GeminiAdapterOptions, type MessageContentBlock, MessageContentBlockSchema, type MockModelOptions, type ModelAdapter, type ModelPricingUsdPerMillionTokens, type ModelToolCall, ModelToolCallSchema, type ModelToolChoice, type ModelToolDefinition, type OpenAiAdapterOptions, type OpenAiCallInput, type OpenAiContentPart, type OpenAiRequestBody, type OpenAiRequestMessage, type OpenAiRequestToolCall, type OpenAiResponseFormat, type OpenAiToolChoice, type OpenAiToolDefinition, type ProviderExtraBody, type ProviderRetryOptions, type ResolveProviderApiKeyInput, type StructuredOutputRequest, type StructuredOutputResult, anthropic, anthropicBatchJsonOnlySystemInstruction, anthropicBatchTraceWorkflowName, applyCacheControlToLastContentBlock, awsSigV4Algorithm, azureOpenai, bedrock, bedrockAnthropicVersion, buildAnthropicCacheControl, buildAnthropicRequestBody, buildAnthropicStructuredOutputJsonSchema, buildAzureOpenAiChatCompletionsUrl, buildAzureOpenAiChatRequestBody, buildBedrockInvokeModelRequestBody, buildBedrockInvokeModelUrl, buildGeminiResponseSchema, buildOpenAiRequestBody, buildOpenAiStructuredOutputJsonSchema, buildProviderCanceledError, calculateProviderRetryDelayMs, convertZodSchemaToJsonSchema, createAnthropicBatchClient, defaultAnthropicBaseUrl, defaultAnthropicBatchPollIntervalMs, defaultAnthropicBatchTimeoutMs, defaultAnthropicMaxOutputTokens, defaultAzureOpenAiApiVersion, defaultBedrockMaxOutputTokens, defaultGeminiBaseUrl, defaultOpenAiBaseUrl, defaultProviderRetryInitialDelayMs, defaultProviderRetryMaxAttempts, defaultProviderRetryMaxDelayMs, estimateModelCallCostUsd, executeAbortableProviderFetch, executeProviderCallWithModelFallback, executeProviderCallWithRetry, extractAnthropicTextContent, extractAnthropicThinkingText, extractAnthropicToolCalls, extractMessageText, gemini, isAbortError, isRetryableProviderAppError, joinSystemMessageText, mapAnthropicResponseToChatCompletionResult, mapAnthropicUsageToTokenUsage, mapAzureOpenAiResponseToChatCompletionResult, mapAzureOpenAiUsageToTokenUsage, mapChatMessageToAnthropicRequestMessage, mapChatMessageToOpenAiRequestMessage, mapModelToolChoiceToAnthropicToolChoice, mapModelToolChoiceToOpenAiToolChoice, mapModelToolDefinitionToOpenAiTool, mapModelToolDefinitionsToAnthropicTools, mergeProviderRequestExtraBody, mockModel, normalizeAnthropicStopReason, normalizeAzureOpenAiFinishReason, openAiStructuredOutputSchemaName, openai, parseAnthropicBatchStructuredContent, parseAnthropicStreamEvents, parseAnthropicToolInputJson, parseAzureOpenAiStreamEvents, parseAzureOpenAiToolArgumentsJson, resolveAnthropicPromptCachingSettings, resolveBedrockCredentials, resolveProviderApiKey, sanitizeJsonSchemaForAnthropicStructuredOutput, sanitizeJsonSchemaForGeminiResponseSchema, sanitizeJsonSchemaForOpenAiStructuredOutput, signAwsRequestWithSigV4, streamAnthropicChatCompletion, streamAzureOpenAiChatCompletion };
809
+ export { type AnthropicAdapterOptions, type AnthropicBatchApiRequest, type AnthropicBatchApiRequestMessage, type AnthropicBatchApiRequestParams, type AnthropicBatchCanceledOutcome, type AnthropicBatchClient, type AnthropicBatchClientOptions, type AnthropicBatchErroredOutcome, type AnthropicBatchExpiredOutcome, type AnthropicBatchMessageRequest, type AnthropicBatchPromptCachingMode, type AnthropicBatchResult, type AnthropicBatchResultOutcome, type AnthropicBatchSubmittedRequestSummary, type AnthropicBatchSucceededOutcome, type AnthropicBatchSystemContentBlock, type AnthropicBatchTraceEventHandler, type AnthropicBatchWaitOptions, type AnthropicCacheControl, type AnthropicCacheTtl, type AnthropicMessageBatch, type AnthropicMessageBatchProcessingStatus, type AnthropicMessageBatchRequestCounts, type AnthropicMessagesResponse, AnthropicMessagesResponseSchema, type AnthropicOutputConfig, type AnthropicPromptCachingMode, type AnthropicPromptCachingOptions, type AnthropicPromptCachingSettings, type AnthropicRequestBody, type AnthropicRequestContentBlock, type AnthropicRequestInput, type AnthropicRequestMessage, type AnthropicRequestSettings, type AnthropicStreamEvent, AnthropicStreamEventSchema, type AnthropicStreamRequestInput, type AnthropicStreamStartUsage, type AnthropicSystemContentBlock, type AnthropicThinkingOptions, type AnthropicToolChoice, type AnthropicToolDefinition, type AnthropicUsage, type AwsCredentials, type AwsSigV4SigningInput, type AzureOpenAiAdapterOptions, type AzureOpenAiChatCompletionResponse, AzureOpenAiChatCompletionResponseSchema, type AzureOpenAiChatRequestBody, type AzureOpenAiStreamRequestBody, type AzureOpenAiStreamRequestInput, type AzureOpenAiUsage, type BedrockAdapterOptions, type BedrockCredentials, type BedrockInvokeModelRequestBody, type ChatCompletionRequest, type ChatCompletionResult, type ChatMessage, type ChatMessageCacheControl, ChatMessageCacheControlSchema, type ChatMessageRole, ChatMessageRoleSchema, ChatMessageSchema, type ChatStopReason, ChatStopReasonSchema, type ChatStreamEvent, type GeminiAdapterOptions, type MessageContentBlock, MessageContentBlockSchema, type MockModelOptions, type ModelAdapter, type ModelPricingUsdPerMillionTokens, type ModelToolCall, ModelToolCallSchema, type ModelToolChoice, type ModelToolDefinition, type OpenAiAdapterOptions, type OpenAiCallInput, type OpenAiContentPart, type OpenAiRequestBody, type OpenAiRequestMessage, type OpenAiRequestToolCall, type OpenAiResponseFormat, type OpenAiStructuredOutputMode, type OpenAiToolChoice, type OpenAiToolDefinition, type ProviderExtraBody, type ProviderRetryOptions, type ResolveProviderApiKeyInput, type StructuredOutputRequest, type StructuredOutputResult, anthropic, anthropicBatchJsonOnlySystemInstruction, anthropicBatchTraceWorkflowName, applyCacheControlToLastContentBlock, awsSigV4Algorithm, azureOpenai, bedrock, bedrockAnthropicVersion, buildAnthropicCacheControl, buildAnthropicRequestBody, buildAnthropicStructuredOutputJsonSchema, buildAzureOpenAiChatCompletionsUrl, buildAzureOpenAiChatRequestBody, buildBedrockInvokeModelRequestBody, buildBedrockInvokeModelUrl, buildGeminiResponseSchema, buildOpenAiRequestBody, buildOpenAiStructuredOutputJsonSchema, buildProviderCanceledError, calculateProviderRetryDelayMs, convertZodSchemaToJsonSchema, createAnthropicBatchClient, defaultAnthropicBaseUrl, defaultAnthropicBatchPollIntervalMs, defaultAnthropicBatchTimeoutMs, defaultAnthropicMaxOutputTokens, defaultAzureOpenAiApiVersion, defaultBedrockMaxOutputTokens, defaultGeminiBaseUrl, defaultOpenAiBaseUrl, defaultProviderRetryInitialDelayMs, defaultProviderRetryMaxAttempts, defaultProviderRetryMaxDelayMs, estimateModelCallCostUsd, executeAbortableProviderFetch, executeProviderCallWithModelFallback, executeProviderCallWithRetry, extractAnthropicTextContent, extractAnthropicThinkingText, extractAnthropicToolCalls, extractMessageText, gemini, isAbortError, isOpenAiReasoningModel, isRetryableProviderAppError, isZodV4Schema, joinSystemMessageText, mapAnthropicResponseToChatCompletionResult, mapAnthropicUsageToTokenUsage, mapAzureOpenAiResponseToChatCompletionResult, mapAzureOpenAiUsageToTokenUsage, mapChatMessageToAnthropicRequestMessage, mapChatMessageToOpenAiRequestMessage, mapModelToolChoiceToAnthropicToolChoice, mapModelToolChoiceToOpenAiToolChoice, mapModelToolDefinitionToOpenAiTool, mapModelToolDefinitionsToAnthropicTools, mergeProviderRequestExtraBody, mockModel, normalizeAnthropicStopReason, normalizeAzureOpenAiFinishReason, openAiStructuredOutputSchemaName, openai, parseAnthropicBatchStructuredContent, parseAnthropicStreamEvents, parseAnthropicToolInputJson, parseAzureOpenAiStreamEvents, parseAzureOpenAiToolArgumentsJson, prependStructuredOutputJsonOnlySystemMessage, resolveAnthropicPromptCachingSettings, resolveBedrockCredentials, resolveOpenAiStructuredOutputMode, resolveProviderApiKey, sanitizeJsonSchemaForAnthropicStructuredOutput, sanitizeJsonSchemaForGeminiResponseSchema, sanitizeJsonSchemaForOpenAiStructuredOutput, signAwsRequestWithSigV4, streamAnthropicChatCompletion, streamAzureOpenAiChatCompletion, structuredOutputJsonOnlySystemInstruction, structuredOutputJsonOnlySystemMessage };
package/dist/index.d.ts CHANGED
@@ -357,12 +357,17 @@ declare function buildProviderCanceledError(providerLabel: string): AppError;
357
357
  declare function executeAbortableProviderFetch(providerLabel: string, performFetch: () => Promise<Response>): Promise<Response>;
358
358
 
359
359
  declare const openAiStructuredOutputSchemaName = "structured_output";
360
+ declare function isZodV4Schema<T>(schema: z.ZodType<T>): boolean;
360
361
  declare function convertZodSchemaToJsonSchema<T>(schema: z.ZodType<T>): JsonObject;
361
362
  declare function buildAnthropicStructuredOutputJsonSchema<T>(schema: z.ZodType<T>): JsonObject;
362
363
  declare function sanitizeJsonSchemaForAnthropicStructuredOutput(jsonSchema: JsonObject): JsonObject;
363
364
  declare function buildOpenAiStructuredOutputJsonSchema<T>(schema: z.ZodType<T>): JsonObject;
364
365
  declare function sanitizeJsonSchemaForOpenAiStructuredOutput(jsonSchema: JsonObject): JsonObject;
365
366
 
367
+ declare const structuredOutputJsonOnlySystemInstruction = "Respond with a single JSON object that satisfies the caller's requested structure. Output only valid JSON with no surrounding text.";
368
+ declare const structuredOutputJsonOnlySystemMessage: ChatMessage;
369
+ declare function prependStructuredOutputJsonOnlySystemMessage(messages: ChatMessage[]): ChatMessage[];
370
+
366
371
  declare function buildGeminiResponseSchema<T>(schema: z.ZodType<T>): JsonObject;
367
372
  declare function sanitizeJsonSchemaForGeminiResponseSchema(jsonSchema: JsonObject): JsonObject;
368
373
 
@@ -539,6 +544,7 @@ type OpenAiRequestBody = {
539
544
  messages: OpenAiRequestMessage[];
540
545
  temperature?: number;
541
546
  max_tokens?: number;
547
+ max_completion_tokens?: number;
542
548
  response_format?: OpenAiResponseFormat;
543
549
  tools?: OpenAiToolDefinition[];
544
550
  tool_choice?: OpenAiToolChoice;
@@ -547,6 +553,7 @@ type OpenAiCallInput = {
547
553
  messages: ChatMessage[];
548
554
  temperature?: number;
549
555
  maxOutputTokens?: number;
556
+ reasoningModel?: boolean;
550
557
  structuredOutputJsonSchema?: JsonObject;
551
558
  requireJsonObjectResponse?: boolean;
552
559
  tools?: ModelToolDefinition[];
@@ -554,6 +561,13 @@ type OpenAiCallInput = {
554
561
  abortSignal?: AbortSignal;
555
562
  extraBody?: Record<string, JsonValue>;
556
563
  };
564
+ type OpenAiStructuredOutputMode = {
565
+ messages: ChatMessage[];
566
+ structuredOutputJsonSchema?: JsonObject;
567
+ requireJsonObjectResponse?: boolean;
568
+ };
569
+ declare function isOpenAiReasoningModel(model: string): boolean;
570
+ declare function resolveOpenAiStructuredOutputMode<T>(messages: ChatMessage[], schema: z.ZodType<T>): OpenAiStructuredOutputMode;
557
571
  declare function buildOpenAiRequestBody(model: string, input: OpenAiCallInput): OpenAiRequestBody;
558
572
  declare function mapModelToolDefinitionToOpenAiTool(tool: ModelToolDefinition): OpenAiToolDefinition;
559
573
  declare function mapModelToolChoiceToOpenAiToolChoice(toolChoice: ModelToolChoice): OpenAiToolChoice;
@@ -695,6 +709,7 @@ type AzureOpenAiAdapterOptions = {
695
709
  fetchImplementation?: typeof fetch;
696
710
  temperature?: number;
697
711
  maxOutputTokens?: number;
712
+ reasoningModel?: boolean;
698
713
  retry?: ProviderRetryOptions;
699
714
  extraBody?: Record<string, JsonValue>;
700
715
  };
@@ -791,4 +806,4 @@ declare function buildBedrockInvokeModelUrl(region: string, modelId: string): st
791
806
  declare function resolveBedrockCredentials(options: BedrockAdapterOptions): BedrockCredentials;
792
807
  declare function buildBedrockInvokeModelRequestBody(input: AnthropicRequestInput): BedrockInvokeModelRequestBody;
793
808
 
794
- export { type AnthropicAdapterOptions, type AnthropicBatchApiRequest, type AnthropicBatchApiRequestMessage, type AnthropicBatchApiRequestParams, type AnthropicBatchCanceledOutcome, type AnthropicBatchClient, type AnthropicBatchClientOptions, type AnthropicBatchErroredOutcome, type AnthropicBatchExpiredOutcome, type AnthropicBatchMessageRequest, type AnthropicBatchPromptCachingMode, type AnthropicBatchResult, type AnthropicBatchResultOutcome, type AnthropicBatchSubmittedRequestSummary, type AnthropicBatchSucceededOutcome, type AnthropicBatchSystemContentBlock, type AnthropicBatchTraceEventHandler, type AnthropicBatchWaitOptions, type AnthropicCacheControl, type AnthropicCacheTtl, type AnthropicMessageBatch, type AnthropicMessageBatchProcessingStatus, type AnthropicMessageBatchRequestCounts, type AnthropicMessagesResponse, AnthropicMessagesResponseSchema, type AnthropicOutputConfig, type AnthropicPromptCachingMode, type AnthropicPromptCachingOptions, type AnthropicPromptCachingSettings, type AnthropicRequestBody, type AnthropicRequestContentBlock, type AnthropicRequestInput, type AnthropicRequestMessage, type AnthropicRequestSettings, type AnthropicStreamEvent, AnthropicStreamEventSchema, type AnthropicStreamRequestInput, type AnthropicStreamStartUsage, type AnthropicSystemContentBlock, type AnthropicThinkingOptions, type AnthropicToolChoice, type AnthropicToolDefinition, type AnthropicUsage, type AwsCredentials, type AwsSigV4SigningInput, type AzureOpenAiAdapterOptions, type AzureOpenAiChatCompletionResponse, AzureOpenAiChatCompletionResponseSchema, type AzureOpenAiChatRequestBody, type AzureOpenAiStreamRequestBody, type AzureOpenAiStreamRequestInput, type AzureOpenAiUsage, type BedrockAdapterOptions, type BedrockCredentials, type BedrockInvokeModelRequestBody, type ChatCompletionRequest, type ChatCompletionResult, type ChatMessage, type ChatMessageCacheControl, ChatMessageCacheControlSchema, type ChatMessageRole, ChatMessageRoleSchema, ChatMessageSchema, type ChatStopReason, ChatStopReasonSchema, type ChatStreamEvent, type GeminiAdapterOptions, type MessageContentBlock, MessageContentBlockSchema, type MockModelOptions, type ModelAdapter, type ModelPricingUsdPerMillionTokens, type ModelToolCall, ModelToolCallSchema, type ModelToolChoice, type ModelToolDefinition, type OpenAiAdapterOptions, type OpenAiCallInput, type OpenAiContentPart, type OpenAiRequestBody, type OpenAiRequestMessage, type OpenAiRequestToolCall, type OpenAiResponseFormat, type OpenAiToolChoice, type OpenAiToolDefinition, type ProviderExtraBody, type ProviderRetryOptions, type ResolveProviderApiKeyInput, type StructuredOutputRequest, type StructuredOutputResult, anthropic, anthropicBatchJsonOnlySystemInstruction, anthropicBatchTraceWorkflowName, applyCacheControlToLastContentBlock, awsSigV4Algorithm, azureOpenai, bedrock, bedrockAnthropicVersion, buildAnthropicCacheControl, buildAnthropicRequestBody, buildAnthropicStructuredOutputJsonSchema, buildAzureOpenAiChatCompletionsUrl, buildAzureOpenAiChatRequestBody, buildBedrockInvokeModelRequestBody, buildBedrockInvokeModelUrl, buildGeminiResponseSchema, buildOpenAiRequestBody, buildOpenAiStructuredOutputJsonSchema, buildProviderCanceledError, calculateProviderRetryDelayMs, convertZodSchemaToJsonSchema, createAnthropicBatchClient, defaultAnthropicBaseUrl, defaultAnthropicBatchPollIntervalMs, defaultAnthropicBatchTimeoutMs, defaultAnthropicMaxOutputTokens, defaultAzureOpenAiApiVersion, defaultBedrockMaxOutputTokens, defaultGeminiBaseUrl, defaultOpenAiBaseUrl, defaultProviderRetryInitialDelayMs, defaultProviderRetryMaxAttempts, defaultProviderRetryMaxDelayMs, estimateModelCallCostUsd, executeAbortableProviderFetch, executeProviderCallWithModelFallback, executeProviderCallWithRetry, extractAnthropicTextContent, extractAnthropicThinkingText, extractAnthropicToolCalls, extractMessageText, gemini, isAbortError, isRetryableProviderAppError, joinSystemMessageText, mapAnthropicResponseToChatCompletionResult, mapAnthropicUsageToTokenUsage, mapAzureOpenAiResponseToChatCompletionResult, mapAzureOpenAiUsageToTokenUsage, mapChatMessageToAnthropicRequestMessage, mapChatMessageToOpenAiRequestMessage, mapModelToolChoiceToAnthropicToolChoice, mapModelToolChoiceToOpenAiToolChoice, mapModelToolDefinitionToOpenAiTool, mapModelToolDefinitionsToAnthropicTools, mergeProviderRequestExtraBody, mockModel, normalizeAnthropicStopReason, normalizeAzureOpenAiFinishReason, openAiStructuredOutputSchemaName, openai, parseAnthropicBatchStructuredContent, parseAnthropicStreamEvents, parseAnthropicToolInputJson, parseAzureOpenAiStreamEvents, parseAzureOpenAiToolArgumentsJson, resolveAnthropicPromptCachingSettings, resolveBedrockCredentials, resolveProviderApiKey, sanitizeJsonSchemaForAnthropicStructuredOutput, sanitizeJsonSchemaForGeminiResponseSchema, sanitizeJsonSchemaForOpenAiStructuredOutput, signAwsRequestWithSigV4, streamAnthropicChatCompletion, streamAzureOpenAiChatCompletion };
809
+ export { type AnthropicAdapterOptions, type AnthropicBatchApiRequest, type AnthropicBatchApiRequestMessage, type AnthropicBatchApiRequestParams, type AnthropicBatchCanceledOutcome, type AnthropicBatchClient, type AnthropicBatchClientOptions, type AnthropicBatchErroredOutcome, type AnthropicBatchExpiredOutcome, type AnthropicBatchMessageRequest, type AnthropicBatchPromptCachingMode, type AnthropicBatchResult, type AnthropicBatchResultOutcome, type AnthropicBatchSubmittedRequestSummary, type AnthropicBatchSucceededOutcome, type AnthropicBatchSystemContentBlock, type AnthropicBatchTraceEventHandler, type AnthropicBatchWaitOptions, type AnthropicCacheControl, type AnthropicCacheTtl, type AnthropicMessageBatch, type AnthropicMessageBatchProcessingStatus, type AnthropicMessageBatchRequestCounts, type AnthropicMessagesResponse, AnthropicMessagesResponseSchema, type AnthropicOutputConfig, type AnthropicPromptCachingMode, type AnthropicPromptCachingOptions, type AnthropicPromptCachingSettings, type AnthropicRequestBody, type AnthropicRequestContentBlock, type AnthropicRequestInput, type AnthropicRequestMessage, type AnthropicRequestSettings, type AnthropicStreamEvent, AnthropicStreamEventSchema, type AnthropicStreamRequestInput, type AnthropicStreamStartUsage, type AnthropicSystemContentBlock, type AnthropicThinkingOptions, type AnthropicToolChoice, type AnthropicToolDefinition, type AnthropicUsage, type AwsCredentials, type AwsSigV4SigningInput, type AzureOpenAiAdapterOptions, type AzureOpenAiChatCompletionResponse, AzureOpenAiChatCompletionResponseSchema, type AzureOpenAiChatRequestBody, type AzureOpenAiStreamRequestBody, type AzureOpenAiStreamRequestInput, type AzureOpenAiUsage, type BedrockAdapterOptions, type BedrockCredentials, type BedrockInvokeModelRequestBody, type ChatCompletionRequest, type ChatCompletionResult, type ChatMessage, type ChatMessageCacheControl, ChatMessageCacheControlSchema, type ChatMessageRole, ChatMessageRoleSchema, ChatMessageSchema, type ChatStopReason, ChatStopReasonSchema, type ChatStreamEvent, type GeminiAdapterOptions, type MessageContentBlock, MessageContentBlockSchema, type MockModelOptions, type ModelAdapter, type ModelPricingUsdPerMillionTokens, type ModelToolCall, ModelToolCallSchema, type ModelToolChoice, type ModelToolDefinition, type OpenAiAdapterOptions, type OpenAiCallInput, type OpenAiContentPart, type OpenAiRequestBody, type OpenAiRequestMessage, type OpenAiRequestToolCall, type OpenAiResponseFormat, type OpenAiStructuredOutputMode, type OpenAiToolChoice, type OpenAiToolDefinition, type ProviderExtraBody, type ProviderRetryOptions, type ResolveProviderApiKeyInput, type StructuredOutputRequest, type StructuredOutputResult, anthropic, anthropicBatchJsonOnlySystemInstruction, anthropicBatchTraceWorkflowName, applyCacheControlToLastContentBlock, awsSigV4Algorithm, azureOpenai, bedrock, bedrockAnthropicVersion, buildAnthropicCacheControl, buildAnthropicRequestBody, buildAnthropicStructuredOutputJsonSchema, buildAzureOpenAiChatCompletionsUrl, buildAzureOpenAiChatRequestBody, buildBedrockInvokeModelRequestBody, buildBedrockInvokeModelUrl, buildGeminiResponseSchema, buildOpenAiRequestBody, buildOpenAiStructuredOutputJsonSchema, buildProviderCanceledError, calculateProviderRetryDelayMs, convertZodSchemaToJsonSchema, createAnthropicBatchClient, defaultAnthropicBaseUrl, defaultAnthropicBatchPollIntervalMs, defaultAnthropicBatchTimeoutMs, defaultAnthropicMaxOutputTokens, defaultAzureOpenAiApiVersion, defaultBedrockMaxOutputTokens, defaultGeminiBaseUrl, defaultOpenAiBaseUrl, defaultProviderRetryInitialDelayMs, defaultProviderRetryMaxAttempts, defaultProviderRetryMaxDelayMs, estimateModelCallCostUsd, executeAbortableProviderFetch, executeProviderCallWithModelFallback, executeProviderCallWithRetry, extractAnthropicTextContent, extractAnthropicThinkingText, extractAnthropicToolCalls, extractMessageText, gemini, isAbortError, isOpenAiReasoningModel, isRetryableProviderAppError, isZodV4Schema, joinSystemMessageText, mapAnthropicResponseToChatCompletionResult, mapAnthropicUsageToTokenUsage, mapAzureOpenAiResponseToChatCompletionResult, mapAzureOpenAiUsageToTokenUsage, mapChatMessageToAnthropicRequestMessage, mapChatMessageToOpenAiRequestMessage, mapModelToolChoiceToAnthropicToolChoice, mapModelToolChoiceToOpenAiToolChoice, mapModelToolDefinitionToOpenAiTool, mapModelToolDefinitionsToAnthropicTools, mergeProviderRequestExtraBody, mockModel, normalizeAnthropicStopReason, normalizeAzureOpenAiFinishReason, openAiStructuredOutputSchemaName, openai, parseAnthropicBatchStructuredContent, parseAnthropicStreamEvents, parseAnthropicToolInputJson, parseAzureOpenAiStreamEvents, parseAzureOpenAiToolArgumentsJson, prependStructuredOutputJsonOnlySystemMessage, resolveAnthropicPromptCachingSettings, resolveBedrockCredentials, resolveOpenAiStructuredOutputMode, resolveProviderApiKey, sanitizeJsonSchemaForAnthropicStructuredOutput, sanitizeJsonSchemaForGeminiResponseSchema, sanitizeJsonSchemaForOpenAiStructuredOutput, signAwsRequestWithSigV4, streamAnthropicChatCompletion, streamAzureOpenAiChatCompletion, structuredOutputJsonOnlySystemInstruction, structuredOutputJsonOnlySystemMessage };
package/dist/index.js CHANGED
@@ -204,6 +204,9 @@ var openAiUnsupportedSchemaKeywords = [
204
204
  "uniqueItems",
205
205
  "default"
206
206
  ];
207
+ function isZodV4Schema(schema) {
208
+ return "_zod" in schema;
209
+ }
207
210
  function convertZodSchemaToJsonSchema(schema) {
208
211
  try {
209
212
  return JsonObjectSchema.parse(z2.toJSONSchema(schema, { io: "input" }));
@@ -268,18 +271,72 @@ function listSchemaPropertyNames(node) {
268
271
  return Object.keys(properties);
269
272
  }
270
273
 
274
+ // src/structured-output-fallback.ts
275
+ var structuredOutputJsonOnlySystemInstruction = "Respond with a single JSON object that satisfies the caller's requested structure. Output only valid JSON with no surrounding text.";
276
+ var structuredOutputJsonOnlySystemMessage = {
277
+ role: "system",
278
+ content: structuredOutputJsonOnlySystemInstruction
279
+ };
280
+ function prependStructuredOutputJsonOnlySystemMessage(messages) {
281
+ return [structuredOutputJsonOnlySystemMessage, ...messages];
282
+ }
283
+
271
284
  // src/openai-request-mapping.ts
285
+ var openAiReasoningModelNamePrefixes = [
286
+ "gpt-5",
287
+ "o1",
288
+ "o3",
289
+ "o4"
290
+ ];
291
+ function isOpenAiReasoningModel(model) {
292
+ return openAiReasoningModelNamePrefixes.some(
293
+ (prefix) => matchesOpenAiModelNamePrefix(model, prefix)
294
+ );
295
+ }
296
+ function matchesOpenAiModelNamePrefix(model, prefix) {
297
+ if (!model.startsWith(prefix)) {
298
+ return false;
299
+ }
300
+ const boundaryCharacter = model.charAt(prefix.length);
301
+ return boundaryCharacter === "" || boundaryCharacter === "-" || boundaryCharacter === ".";
302
+ }
303
+ function resolveOpenAiStructuredOutputMode(messages, schema) {
304
+ if (isZodV4Schema(schema)) {
305
+ return {
306
+ messages,
307
+ structuredOutputJsonSchema: buildOpenAiStructuredOutputJsonSchema(schema)
308
+ };
309
+ }
310
+ return {
311
+ messages: prependStructuredOutputJsonOnlySystemMessage(messages),
312
+ requireJsonObjectResponse: true
313
+ };
314
+ }
272
315
  function buildOpenAiRequestBody(model, input) {
273
316
  const requestBody = {
274
317
  model,
275
318
  messages: input.messages.map(mapChatMessageToOpenAiRequestMessage)
276
319
  };
277
- if (input.temperature !== void 0) {
320
+ applyOpenAiGenerationSettings(requestBody, model, input);
321
+ applyOpenAiResponseFormat(requestBody, input);
322
+ applyOpenAiToolFields(requestBody, input);
323
+ return requestBody;
324
+ }
325
+ function applyOpenAiGenerationSettings(requestBody, model, input) {
326
+ const reasoningModel = input.reasoningModel ?? isOpenAiReasoningModel(model);
327
+ if (input.temperature !== void 0 && !reasoningModel) {
278
328
  requestBody.temperature = input.temperature;
279
329
  }
280
- if (input.maxOutputTokens !== void 0) {
281
- requestBody.max_tokens = input.maxOutputTokens;
330
+ if (input.maxOutputTokens === void 0) {
331
+ return;
332
+ }
333
+ if (reasoningModel) {
334
+ requestBody.max_completion_tokens = input.maxOutputTokens;
335
+ return;
282
336
  }
337
+ requestBody.max_tokens = input.maxOutputTokens;
338
+ }
339
+ function applyOpenAiResponseFormat(requestBody, input) {
283
340
  if (input.structuredOutputJsonSchema !== void 0) {
284
341
  requestBody.response_format = {
285
342
  type: "json_schema",
@@ -289,9 +346,13 @@ function buildOpenAiRequestBody(model, input) {
289
346
  schema: input.structuredOutputJsonSchema
290
347
  }
291
348
  };
292
- } else if (input.requireJsonObjectResponse === true) {
349
+ return;
350
+ }
351
+ if (input.requireJsonObjectResponse === true) {
293
352
  requestBody.response_format = { type: "json_object" };
294
353
  }
354
+ }
355
+ function applyOpenAiToolFields(requestBody, input) {
295
356
  if (input.tools !== void 0 && input.tools.length > 0) {
296
357
  requestBody.tools = input.tools.map(mapModelToolDefinitionToOpenAiTool);
297
358
  }
@@ -300,7 +361,6 @@ function buildOpenAiRequestBody(model, input) {
300
361
  input.toolChoice
301
362
  );
302
363
  }
303
- return requestBody;
304
364
  }
305
365
  function mapModelToolDefinitionToOpenAiTool(tool) {
306
366
  return {
@@ -665,12 +725,9 @@ function openai(options) {
665
725
  },
666
726
  async generateStructuredOutput(request) {
667
727
  const completion = await executeOpenAiAdapterCall(options, {
668
- messages: request.messages,
728
+ ...resolveOpenAiStructuredOutputMode(request.messages, request.schema),
669
729
  temperature: request.temperature ?? options.temperature,
670
730
  maxOutputTokens: request.maxOutputTokens ?? options.maxOutputTokens,
671
- structuredOutputJsonSchema: buildOpenAiStructuredOutputJsonSchema(
672
- request.schema
673
- ),
674
731
  abortSignal: request.abortSignal
675
732
  });
676
733
  return {
@@ -1438,15 +1495,10 @@ function anthropic(options) {
1438
1495
  );
1439
1496
  },
1440
1497
  async generateStructuredOutput(request) {
1441
- const completion = await executeAnthropicAdapterCall(options, {
1442
- messages: request.messages,
1443
- temperature: request.temperature ?? options.temperature,
1444
- maxOutputTokens: request.maxOutputTokens ?? options.maxOutputTokens,
1445
- structuredOutputJsonSchema: buildAnthropicStructuredOutputJsonSchema(
1446
- request.schema
1447
- ),
1448
- abortSignal: request.abortSignal
1449
- });
1498
+ const completion = await executeAnthropicAdapterCall(
1499
+ options,
1500
+ buildAnthropicStructuredOutputInput(options, request)
1501
+ );
1450
1502
  return {
1451
1503
  value: parseStructuredJsonText(completion.content, request.schema),
1452
1504
  tokenUsage: completion.tokenUsage,
@@ -1460,6 +1512,26 @@ function anthropic(options) {
1460
1512
  }
1461
1513
  };
1462
1514
  }
1515
+ function buildAnthropicStructuredOutputInput(options, request) {
1516
+ const baseInput = {
1517
+ messages: request.messages,
1518
+ temperature: request.temperature ?? options.temperature,
1519
+ maxOutputTokens: request.maxOutputTokens ?? options.maxOutputTokens,
1520
+ abortSignal: request.abortSignal
1521
+ };
1522
+ if (isZodV4Schema(request.schema)) {
1523
+ return {
1524
+ ...baseInput,
1525
+ structuredOutputJsonSchema: buildAnthropicStructuredOutputJsonSchema(
1526
+ request.schema
1527
+ )
1528
+ };
1529
+ }
1530
+ return {
1531
+ ...baseInput,
1532
+ messages: prependStructuredOutputJsonOnlySystemMessage(request.messages)
1533
+ };
1534
+ }
1463
1535
  function buildAnthropicCallInput(options, request) {
1464
1536
  return {
1465
1537
  messages: request.messages,
@@ -1684,13 +1756,10 @@ function gemini(options) {
1684
1756
  });
1685
1757
  },
1686
1758
  async generateStructuredOutput(request) {
1687
- const completion = await executeGeminiAdapterCall(options, {
1688
- messages: request.messages,
1689
- temperature: request.temperature ?? options.temperature,
1690
- maxOutputTokens: request.maxOutputTokens ?? options.maxOutputTokens,
1691
- responseSchema: buildGeminiResponseSchema(request.schema),
1692
- abortSignal: request.abortSignal
1693
- });
1759
+ const completion = await executeGeminiAdapterCall(
1760
+ options,
1761
+ buildGeminiStructuredOutputInput(options, request)
1762
+ );
1694
1763
  return {
1695
1764
  value: parseStructuredJsonText(completion.content, request.schema),
1696
1765
  tokenUsage: completion.tokenUsage,
@@ -1699,6 +1768,25 @@ function gemini(options) {
1699
1768
  }
1700
1769
  };
1701
1770
  }
1771
+ function buildGeminiStructuredOutputInput(options, request) {
1772
+ const baseInput = {
1773
+ messages: request.messages,
1774
+ temperature: request.temperature ?? options.temperature,
1775
+ maxOutputTokens: request.maxOutputTokens ?? options.maxOutputTokens,
1776
+ abortSignal: request.abortSignal
1777
+ };
1778
+ if (isZodV4Schema(request.schema)) {
1779
+ return {
1780
+ ...baseInput,
1781
+ responseSchema: buildGeminiResponseSchema(request.schema)
1782
+ };
1783
+ }
1784
+ return {
1785
+ ...baseInput,
1786
+ messages: prependStructuredOutputJsonOnlySystemMessage(request.messages),
1787
+ requireJsonResponse: true
1788
+ };
1789
+ }
1702
1790
  async function executeGeminiAdapterCall(options, input) {
1703
1791
  return await executeProviderCallWithModelFallback(
1704
1792
  options.fallbackModel,
@@ -1816,6 +1904,8 @@ function buildGeminiGenerationConfig(input) {
1816
1904
  if (input.responseSchema !== void 0) {
1817
1905
  generationConfig.responseMimeType = "application/json";
1818
1906
  generationConfig.responseSchema = input.responseSchema;
1907
+ } else if (input.requireJsonResponse === true) {
1908
+ generationConfig.responseMimeType = "application/json";
1819
1909
  }
1820
1910
  if (Object.keys(generationConfig).length === 0) {
1821
1911
  return void 0;
@@ -2023,7 +2113,7 @@ import { AppError as AppError15 } from "@assemble-dev/shared-utils/errors";
2023
2113
  import { AppErrorCode as AppErrorCode13 } from "@assemble-dev/shared-types/errors";
2024
2114
  import { JsonValueSchema as JsonValueSchema6 } from "@assemble-dev/shared-types/json";
2025
2115
  import { AppError as AppError14 } from "@assemble-dev/shared-utils/errors";
2026
- var anthropicBatchJsonOnlySystemInstruction = "Respond with a single JSON object that satisfies the caller's requested structure. Output only valid JSON with no surrounding text.";
2116
+ var anthropicBatchJsonOnlySystemInstruction = structuredOutputJsonOnlySystemInstruction;
2027
2117
  function parseAnthropicBatchStructuredContent(content, schema) {
2028
2118
  const jsonValue = parseStructuredJsonText(
2029
2119
  content,
@@ -2833,6 +2923,7 @@ function azureOpenai(options) {
2833
2923
  messages: request.messages,
2834
2924
  temperature: request.temperature ?? options.temperature,
2835
2925
  maxOutputTokens: request.maxOutputTokens ?? options.maxOutputTokens,
2926
+ reasoningModel: options.reasoningModel,
2836
2927
  tools: request.tools,
2837
2928
  toolChoice: request.toolChoice,
2838
2929
  abortSignal: request.abortSignal,
@@ -2841,12 +2932,10 @@ function azureOpenai(options) {
2841
2932
  },
2842
2933
  async generateStructuredOutput(request) {
2843
2934
  const completion = await executeAzureOpenAiAdapterCall(options, {
2844
- messages: request.messages,
2935
+ ...resolveOpenAiStructuredOutputMode(request.messages, request.schema),
2845
2936
  temperature: request.temperature ?? options.temperature,
2846
2937
  maxOutputTokens: request.maxOutputTokens ?? options.maxOutputTokens,
2847
- structuredOutputJsonSchema: buildOpenAiStructuredOutputJsonSchema(
2848
- request.schema
2849
- ),
2938
+ reasoningModel: options.reasoningModel,
2850
2939
  abortSignal: request.abortSignal
2851
2940
  });
2852
2941
  return {
@@ -2945,6 +3034,7 @@ function buildAzureOpenAiStreamInput(options, request) {
2945
3034
  messages: request.messages,
2946
3035
  temperature: request.temperature ?? options.temperature,
2947
3036
  maxOutputTokens: request.maxOutputTokens ?? options.maxOutputTokens,
3037
+ reasoningModel: options.reasoningModel,
2948
3038
  tools: request.tools,
2949
3039
  toolChoice: request.toolChoice,
2950
3040
  abortSignal: request.abortSignal,
@@ -3239,6 +3329,9 @@ function buildBedrockInvokeModelRequestBody(input) {
3239
3329
  };
3240
3330
  }
3241
3331
  function buildBedrockStructuredOutputSystemMessage(schema) {
3332
+ if (!isZodV4Schema(schema)) {
3333
+ return structuredOutputJsonOnlySystemMessage;
3334
+ }
3242
3335
  return {
3243
3336
  role: "system",
3244
3337
  content: `Respond with a single JSON object that satisfies the following JSON Schema. Output only valid JSON with no surrounding text.
@@ -3300,7 +3393,9 @@ export {
3300
3393
  extractMessageText,
3301
3394
  gemini,
3302
3395
  isAbortError,
3396
+ isOpenAiReasoningModel,
3303
3397
  isRetryableProviderAppError,
3398
+ isZodV4Schema,
3304
3399
  joinSystemMessageText,
3305
3400
  mapAnthropicResponseToChatCompletionResult,
3306
3401
  mapAnthropicUsageToTokenUsage,
@@ -3323,13 +3418,17 @@ export {
3323
3418
  parseAnthropicToolInputJson,
3324
3419
  parseAzureOpenAiStreamEvents,
3325
3420
  parseAzureOpenAiToolArgumentsJson,
3421
+ prependStructuredOutputJsonOnlySystemMessage,
3326
3422
  resolveAnthropicPromptCachingSettings,
3327
3423
  resolveBedrockCredentials,
3424
+ resolveOpenAiStructuredOutputMode,
3328
3425
  resolveProviderApiKey,
3329
3426
  sanitizeJsonSchemaForAnthropicStructuredOutput,
3330
3427
  sanitizeJsonSchemaForGeminiResponseSchema,
3331
3428
  sanitizeJsonSchemaForOpenAiStructuredOutput,
3332
3429
  signAwsRequestWithSigV4,
3333
3430
  streamAnthropicChatCompletion,
3334
- streamAzureOpenAiChatCompletion
3431
+ streamAzureOpenAiChatCompletion,
3432
+ structuredOutputJsonOnlySystemInstruction,
3433
+ structuredOutputJsonOnlySystemMessage
3335
3434
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@assemble-dev/providers",
3
- "version": "0.3.0",
3
+ "version": "0.3.1",
4
4
  "license": "Elastic-2.0",
5
5
  "description": "Model provider adapters for the Assemble SDK",
6
6
  "repository": {
@@ -27,8 +27,8 @@
27
27
  ],
28
28
  "dependencies": {
29
29
  "zod": "^4.1.13",
30
- "@assemble-dev/shared-types": "0.3.0",
31
- "@assemble-dev/shared-utils": "0.3.0"
30
+ "@assemble-dev/shared-types": "0.3.1",
31
+ "@assemble-dev/shared-utils": "0.3.1"
32
32
  },
33
33
  "devDependencies": {
34
34
  "@types/node": "^20.19.0",