genai-lite 0.13.1 → 0.14.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.
@@ -53,14 +53,6 @@ export declare class AnthropicClientAdapter implements ILLMClientAdapter {
53
53
  private mapAnthropicUsage;
54
54
  private mergeAnthropicUsage;
55
55
  private createSyntheticMessage;
56
- /**
57
- * Recursively adds additionalProperties: false to all object schemas
58
- * Required by Anthropic's strict mode for structured outputs
59
- *
60
- * @param schema - The JSON schema to process
61
- * @returns A new schema with additionalProperties: false on all objects
62
- */
63
- private addAdditionalPropertiesFalse;
64
56
  /**
65
57
  * Formats messages for Anthropic API with proper system message handling
66
58
  *
@@ -9,6 +9,7 @@ exports.AnthropicClientAdapter = void 0;
9
9
  const sdk_1 = __importDefault(require("@anthropic-ai/sdk"));
10
10
  const types_1 = require("./types");
11
11
  const errorUtils_1 = require("../../shared/adapters/errorUtils");
12
+ const schemaUtils_1 = require("../../shared/adapters/schemaUtils");
12
13
  const systemMessageUtils_1 = require("../../shared/adapters/systemMessageUtils");
13
14
  const defaultLogger_1 = require("../../logging/defaultLogger");
14
15
  /**
@@ -207,7 +208,12 @@ class AnthropicClientAdapter {
207
208
  };
208
209
  }
209
210
  prepareMessageRequest(request, apiKey, options) {
210
- // Check if structured output is requested - need beta API
211
+ const hasTemperature = request.settings.temperature !== undefined;
212
+ const hasTopP = request.settings.topP !== undefined;
213
+ if (hasTemperature && hasTopP) {
214
+ throw new Error("Invalid Anthropic settings: temperature and topP cannot both be specified");
215
+ }
216
+ // Check if generally available structured output is requested.
211
217
  const useStructuredOutput = !!(request.settings.structuredOutput?.schema &&
212
218
  request.settings.structuredOutput.enabled !== false);
213
219
  // Initialize Anthropic client
@@ -228,8 +234,8 @@ class AnthropicClientAdapter {
228
234
  model: request.modelId,
229
235
  messages: messages,
230
236
  max_tokens: request.settings.maxTokens,
231
- temperature: request.settings.temperature,
232
- top_p: request.settings.topP,
237
+ ...(hasTemperature && { temperature: request.settings.temperature }),
238
+ ...(hasTopP && { top_p: request.settings.topP }),
233
239
  ...(request.settings.topK !== undefined && {
234
240
  top_k: request.settings.topK,
235
241
  }),
@@ -247,7 +253,7 @@ class AnthropicClientAdapter {
247
253
  const so = request.settings.structuredOutput;
248
254
  // Anthropic requires additionalProperties: false on all object schemas
249
255
  const processedSchema = so.strict !== false
250
- ? this.addAdditionalPropertiesFalse(so.schema)
256
+ ? (0, schemaUtils_1.applyStrictSchemaConstraints)({ ...so.schema })
251
257
  : { ...so.schema };
252
258
  messageParams.output_config = {
253
259
  format: {
@@ -344,35 +350,6 @@ class AnthropicClientAdapter {
344
350
  usage: accumulator.usage,
345
351
  };
346
352
  }
347
- /**
348
- * Recursively adds additionalProperties: false to all object schemas
349
- * Required by Anthropic's strict mode for structured outputs
350
- *
351
- * @param schema - The JSON schema to process
352
- * @returns A new schema with additionalProperties: false on all objects
353
- */
354
- addAdditionalPropertiesFalse(schema) {
355
- if (!schema || typeof schema !== 'object') {
356
- return schema;
357
- }
358
- const processed = { ...schema };
359
- // If this is an object type, add additionalProperties: false
360
- if (processed.type === 'object') {
361
- processed.additionalProperties = false;
362
- // Process nested properties
363
- if (processed.properties) {
364
- processed.properties = {};
365
- for (const [key, value] of Object.entries(schema.properties)) {
366
- processed.properties[key] = this.addAdditionalPropertiesFalse(value);
367
- }
368
- }
369
- }
370
- // Process array items
371
- if (processed.items) {
372
- processed.items = this.addAdditionalPropertiesFalse(schema.items);
373
- }
374
- return processed;
375
- }
376
353
  /**
377
354
  * Formats messages for Anthropic API with proper system message handling
378
355
  *
@@ -490,24 +467,15 @@ class AnthropicClientAdapter {
490
467
  if (textBlocks.length === 0) {
491
468
  throw new Error("Invalid completion structure from Anthropic API");
492
469
  }
493
- // Extract thinking/reasoning content if available
494
- let reasoning;
495
- let reasoning_details;
496
- // Check for thinking content in the response
497
- if (completion.thinking_content) {
498
- reasoning = completion.thinking_content;
499
- }
470
+ // Extract reasoning from thinking content blocks. This is the only place
471
+ // Anthropic exposes it: `Message` has no `thinking_content` or
472
+ // `reasoning_details` field (the latter is an OpenRouter concept, handled in
473
+ // OpenRouterClientAdapter), so branches reading those could never fire.
500
474
  const thinkingContent = completion.content
501
475
  .filter((block) => block.type === "thinking" && typeof block.thinking === "string")
502
476
  .map((block) => block.thinking)
503
477
  .join("");
504
- if (!reasoning && thinkingContent) {
505
- reasoning = thinkingContent;
506
- }
507
- // Check for reasoning details that need to be preserved
508
- if (completion.reasoning_details) {
509
- reasoning_details = completion.reasoning_details;
510
- }
478
+ const reasoning = thinkingContent || undefined;
511
479
  // Map Anthropic's stop reason to our standard format
512
480
  const finishReason = this.mapAnthropicStopReason(completion.stop_reason);
513
481
  const choice = {
@@ -522,10 +490,6 @@ class AnthropicClientAdapter {
522
490
  if (reasoning && request.settings.reasoning && !request.settings.reasoning.exclude) {
523
491
  choice.reasoning = reasoning;
524
492
  }
525
- // Always include reasoning_details if present (for tool use continuation)
526
- if (reasoning_details) {
527
- choice.reasoning_details = reasoning_details;
528
- }
529
493
  return {
530
494
  id: completion.id,
531
495
  provider: request.providerId,
@@ -551,12 +515,22 @@ class AnthropicClientAdapter {
551
515
  mapAnthropicStopReason(anthropicReason) {
552
516
  if (!anthropicReason)
553
517
  return null;
518
+ // Anthropic's StopReason union is exactly:
519
+ // end_turn | max_tokens | stop_sequence | tool_use | pause_turn | refusal
520
+ // Note there is no `content_filter` - that is OpenAI's vocabulary. A model
521
+ // that declines on policy grounds reports `refusal`, which we normalize to
522
+ // `content_filter` so callers can detect a blocked completion with the same
523
+ // check they already use for OpenAI and Gemini.
554
524
  const reasonMap = {
555
525
  end_turn: "stop",
556
526
  max_tokens: "length",
557
527
  stop_sequence: "stop",
558
- content_filter: "content_filter",
559
528
  tool_use: "tool_calls",
529
+ refusal: "content_filter",
530
+ // Anthropic-specific: a long-running turn was paused and the caller is
531
+ // expected to continue it. No equivalent in the normalized vocabulary, so
532
+ // it maps to "other" deliberately rather than by falling through.
533
+ pause_turn: "other",
560
534
  };
561
535
  return reasonMap[anthropicReason] || "other";
562
536
  }
@@ -52,14 +52,6 @@ export declare class OpenAIClientAdapter implements ILLMClientAdapter {
52
52
  private createTransportOptions;
53
53
  private getStreamChoiceState;
54
54
  private createSyntheticCompletion;
55
- /**
56
- * Recursively adds additionalProperties: false to all object schemas
57
- * Required by OpenAI's strict mode for structured outputs
58
- *
59
- * @param schema - The JSON schema to process
60
- * @returns A new schema with additionalProperties: false on all objects
61
- */
62
- private addAdditionalPropertiesFalse;
63
55
  /**
64
56
  * Formats messages for OpenAI API
65
57
  *
@@ -10,6 +10,7 @@ const openai_1 = __importDefault(require("openai"));
10
10
  const types_1 = require("./types");
11
11
  const errorUtils_1 = require("../../shared/adapters/errorUtils");
12
12
  const logprobsUtils_1 = require("../../shared/adapters/logprobsUtils");
13
+ const schemaUtils_1 = require("../../shared/adapters/schemaUtils");
13
14
  const systemMessageUtils_1 = require("../../shared/adapters/systemMessageUtils");
14
15
  const defaultLogger_1 = require("../../logging/defaultLogger");
15
16
  /**
@@ -231,8 +232,10 @@ class OpenAIClientAdapter {
231
232
  }
232
233
  if (request.settings.structuredOutput?.schema && request.settings.structuredOutput.enabled !== false) {
233
234
  const so = request.settings.structuredOutput;
235
+ // OpenAI strict mode additionally requires `required` to list every
236
+ // property of each object schema.
234
237
  const processedSchema = so.strict !== false
235
- ? this.addAdditionalPropertiesFalse(so.schema)
238
+ ? (0, schemaUtils_1.applyStrictSchemaConstraints)({ ...so.schema }, { requireAllProperties: true })
236
239
  : so.schema;
237
240
  completionParams.response_format = {
238
241
  type: 'json_schema',
@@ -288,38 +291,6 @@ class OpenAIClientAdapter {
288
291
  ...(usage && { usage }),
289
292
  };
290
293
  }
291
- /**
292
- * Recursively adds additionalProperties: false to all object schemas
293
- * Required by OpenAI's strict mode for structured outputs
294
- *
295
- * @param schema - The JSON schema to process
296
- * @returns A new schema with additionalProperties: false on all objects
297
- */
298
- addAdditionalPropertiesFalse(schema) {
299
- if (!schema || typeof schema !== 'object') {
300
- return schema;
301
- }
302
- const processed = { ...schema };
303
- // If this is an object type, add additionalProperties: false and ensure required includes all properties
304
- if (processed.type === 'object') {
305
- processed.additionalProperties = false;
306
- // Process nested properties
307
- if (processed.properties) {
308
- const propertyKeys = Object.keys(schema.properties);
309
- processed.properties = {};
310
- for (const [key, value] of Object.entries(schema.properties)) {
311
- processed.properties[key] = this.addAdditionalPropertiesFalse(value);
312
- }
313
- // OpenAI strict mode requires 'required' to include ALL properties
314
- processed.required = propertyKeys;
315
- }
316
- }
317
- // Process array items
318
- if (processed.items) {
319
- processed.items = this.addAdditionalPropertiesFalse(schema.items);
320
- }
321
- return processed;
322
- }
323
294
  /**
324
295
  * Formats messages for OpenAI API
325
296
  *
@@ -77,6 +77,11 @@ class RequestValidator {
77
77
  */
78
78
  validateSettings(settings, providerId, modelId) {
79
79
  const settingsValidationErrors = (0, config_1.validateLLMSettings)(settings);
80
+ if (providerId === "anthropic" &&
81
+ settings.temperature !== undefined &&
82
+ settings.topP !== undefined) {
83
+ settingsValidationErrors.push("Anthropic requests cannot specify both temperature and topP; choose one sampler");
84
+ }
80
85
  if (settingsValidationErrors.length > 0) {
81
86
  return {
82
87
  provider: providerId,
@@ -72,6 +72,20 @@ class SettingsManager {
72
72
  }
73
73
  : undefined,
74
74
  };
75
+ if (providerId === "anthropic") {
76
+ const hasExplicitTemperature = requestSettings?.temperature !== undefined;
77
+ const hasExplicitTopP = requestSettings?.topP !== undefined;
78
+ // Anthropic models may reject requests containing both samplers. Preserve
79
+ // the caller-selected sampler; otherwise retain only the temperature
80
+ // default. Explicit conflicts are rejected earlier by RequestValidator,
81
+ // while this precedence keeps direct SettingsManager use transport-safe.
82
+ if (hasExplicitTopP && !hasExplicitTemperature) {
83
+ delete mergedSettings.temperature;
84
+ }
85
+ else {
86
+ delete mergedSettings.topP;
87
+ }
88
+ }
75
89
  // Log the final settings for debugging
76
90
  this.logger.debug(`Merged settings for ${providerId}/${modelId}:`, {
77
91
  temperature: mergedSettings.temperature,
@@ -304,11 +304,17 @@ export interface OpenRouterProviderSettings {
304
304
  * Configurable settings for LLM requests
305
305
  */
306
306
  export interface LLMSettings {
307
- /** Controls randomness in the response (0.0 to 2.0, typically 0.0 to 1.0) */
307
+ /**
308
+ * Controls randomness in the response (0.0 to 2.0, typically 0.0 to 1.0).
309
+ * Mutually exclusive with topP for Anthropic requests.
310
+ */
308
311
  temperature?: number;
309
312
  /** Maximum number of tokens to generate in the response */
310
313
  maxTokens?: number;
311
- /** Controls diversity via nucleus sampling (0.0 to 1.0) */
314
+ /**
315
+ * Controls diversity via nucleus sampling (0.0 to 1.0).
316
+ * Mutually exclusive with temperature for Anthropic requests.
317
+ */
312
318
  topP?: number;
313
319
  /** Sequences where the API will stop generating further tokens */
314
320
  stopSequences?: string[];
@@ -0,0 +1,32 @@
1
+ export interface StrictSchemaOptions {
2
+ /**
3
+ * When true, every object schema's `required` is rewritten to list all of its
4
+ * properties. OpenAI's strict mode demands this; Anthropic's does not.
5
+ *
6
+ * @default false
7
+ */
8
+ requireAllProperties?: boolean;
9
+ }
10
+ /**
11
+ * Returns a deep copy of `schema` with `additionalProperties: false` set on every
12
+ * object schema, as required by OpenAI's and Anthropic's strict structured-output
13
+ * modes.
14
+ *
15
+ * The input is never mutated.
16
+ *
17
+ * Traversal covers `properties`, `items` (single or tuple form), `prefixItems`,
18
+ * `$defs`, `definitions`, `anyOf`, `oneOf`, `allOf`, and `not`. Note that `$ref`
19
+ * pointers are **not** resolved — they are left as-is, and the schemas they point
20
+ * at are constrained where they are *defined* (typically under `$defs`), which is
21
+ * what the providers validate. Both providers reject genuinely recursive schemas
22
+ * anyway, so resolving refs would buy nothing.
23
+ *
24
+ * Cyclic *JavaScript* object graphs (a schema object reachable from itself) are
25
+ * handled: each input node is copied at most once and revisits reuse that copy,
26
+ * so the walk terminates and shared subschemas stay shared.
27
+ *
28
+ * @param schema - The JSON schema to process
29
+ * @param options - Provider-specific strictness tweaks
30
+ * @returns A new schema with strict-mode constraints applied
31
+ */
32
+ export declare function applyStrictSchemaConstraints<T>(schema: T, options?: StrictSchemaOptions): T;
@@ -0,0 +1,87 @@
1
+ "use strict";
2
+ // AI Summary: Shared JSON Schema preparation for providers whose strict
3
+ // structured-output mode requires additionalProperties: false on every object
4
+ // schema. Traverses properties, items (incl. tuple form), $defs/definitions,
5
+ // anyOf/oneOf/allOf, prefixItems and not, and is safe against cyclic input.
6
+ Object.defineProperty(exports, "__esModule", { value: true });
7
+ exports.applyStrictSchemaConstraints = applyStrictSchemaConstraints;
8
+ /** Keywords whose value is a map of name -> subschema. */
9
+ const SUBSCHEMA_MAPS = ["properties", "$defs", "definitions"];
10
+ /** Keywords whose value is an array of subschemas. */
11
+ const SUBSCHEMA_ARRAYS = ["anyOf", "oneOf", "allOf", "prefixItems"];
12
+ /**
13
+ * Returns a deep copy of `schema` with `additionalProperties: false` set on every
14
+ * object schema, as required by OpenAI's and Anthropic's strict structured-output
15
+ * modes.
16
+ *
17
+ * The input is never mutated.
18
+ *
19
+ * Traversal covers `properties`, `items` (single or tuple form), `prefixItems`,
20
+ * `$defs`, `definitions`, `anyOf`, `oneOf`, `allOf`, and `not`. Note that `$ref`
21
+ * pointers are **not** resolved — they are left as-is, and the schemas they point
22
+ * at are constrained where they are *defined* (typically under `$defs`), which is
23
+ * what the providers validate. Both providers reject genuinely recursive schemas
24
+ * anyway, so resolving refs would buy nothing.
25
+ *
26
+ * Cyclic *JavaScript* object graphs (a schema object reachable from itself) are
27
+ * handled: each input node is copied at most once and revisits reuse that copy,
28
+ * so the walk terminates and shared subschemas stay shared.
29
+ *
30
+ * @param schema - The JSON schema to process
31
+ * @param options - Provider-specific strictness tweaks
32
+ * @returns A new schema with strict-mode constraints applied
33
+ */
34
+ function applyStrictSchemaConstraints(schema, options = {}) {
35
+ return walk(schema, options, new Map());
36
+ }
37
+ function walk(node, options, seen) {
38
+ if (!node || typeof node !== "object") {
39
+ return node;
40
+ }
41
+ const cached = seen.get(node);
42
+ if (cached !== undefined) {
43
+ return cached;
44
+ }
45
+ if (Array.isArray(node)) {
46
+ const copy = [];
47
+ seen.set(node, copy);
48
+ for (const entry of node) {
49
+ copy.push(walk(entry, options, seen));
50
+ }
51
+ return copy;
52
+ }
53
+ const source = node;
54
+ const processed = { ...source };
55
+ // Registered before recursing so a cycle resolves to this same object.
56
+ seen.set(node, processed);
57
+ if (source.type === "object") {
58
+ processed.additionalProperties = false;
59
+ const properties = source.properties;
60
+ if (options.requireAllProperties && properties && typeof properties === "object") {
61
+ processed.required = Object.keys(properties);
62
+ }
63
+ }
64
+ for (const key of SUBSCHEMA_MAPS) {
65
+ const value = source[key];
66
+ if (value && typeof value === "object" && !Array.isArray(value)) {
67
+ const out = {};
68
+ for (const [name, subschema] of Object.entries(value)) {
69
+ out[name] = walk(subschema, options, seen);
70
+ }
71
+ processed[key] = out;
72
+ }
73
+ }
74
+ for (const key of SUBSCHEMA_ARRAYS) {
75
+ const value = source[key];
76
+ if (Array.isArray(value)) {
77
+ processed[key] = value.map((subschema) => walk(subschema, options, seen));
78
+ }
79
+ }
80
+ if (source.items !== undefined) {
81
+ processed.items = walk(source.items, options, seen);
82
+ }
83
+ if (source.not !== undefined) {
84
+ processed.not = walk(source.not, options, seen);
85
+ }
86
+ return processed;
87
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "genai-lite",
3
- "version": "0.13.1",
3
+ "version": "0.14.1",
4
4
  "description": "A lightweight, portable toolkit for interacting with various Generative AI APIs.",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -46,7 +46,7 @@
46
46
  "test:e2e:reasoning": "npm run build && jest --config jest.e2e.config.js reasoning.e2e.test.ts"
47
47
  },
48
48
  "dependencies": {
49
- "@anthropic-ai/sdk": "^0.110.0",
49
+ "@anthropic-ai/sdk": "^0.115.0",
50
50
  "@google/genai": "^2.10.0",
51
51
  "@mistralai/mistralai": "^1.15.1",
52
52
  "js-tiktoken": "^1.0.20",