






















@@ -144,6 +144,86 @@ function isTrulyEmptySchema(schemaRecord: Record<string, unknown>): boolean {
144144return Object.keys(schemaRecord).length === 0;
145145}
146146147+function normalizeArraySchemasMissingItems(schema: unknown): unknown {
148+if (!isSchemaRecord(schema)) {
149+return schema;
150+}
151+152+let changed = false;
153+const nextSchema: Record<string, unknown> = { ...schema };
154+if (nextSchema.type === "array" && nextSchema.items === undefined) {
155+nextSchema.items = {};
156+changed = true;
157+}
158+159+const normalizeSchemaValue = (key: string): void => {
160+if (!(key in nextSchema)) {
161+return;
162+}
163+const value = nextSchema[key];
164+if (Array.isArray(value)) {
165+const normalized = value.map(normalizeArraySchemasMissingItems);
166+if (normalized.some((entry, index) => entry !== value[index])) {
167+nextSchema[key] = normalized;
168+changed = true;
169+}
170+return;
171+}
172+173+const normalized = normalizeArraySchemasMissingItems(value);
174+if (normalized !== value) {
175+nextSchema[key] = normalized;
176+changed = true;
177+}
178+};
179+180+for (const key of [
181+"items",
182+"contains",
183+"additionalProperties",
184+"propertyNames",
185+"not",
186+"if",
187+"then",
188+"else",
189+]) {
190+normalizeSchemaValue(key);
191+}
192+193+for (const key of ["anyOf", "oneOf", "allOf", "prefixItems"]) {
194+normalizeSchemaValue(key);
195+}
196+197+for (const key of [
198+"properties",
199+"patternProperties",
200+"dependentSchemas",
201+"$defs",
202+"definitions",
203+]) {
204+const value = nextSchema[key];
205+if (!isSchemaRecord(value)) {
206+continue;
207+}
208+let entriesChanged = false;
209+const normalizedEntries: Array<[string, unknown]> = Object.entries(value).map(
210+([entryKey, entryValue]) => {
211+const normalizedEntryValue = normalizeArraySchemasMissingItems(entryValue);
212+if (normalizedEntryValue !== entryValue) {
213+entriesChanged = true;
214+}
215+return [entryKey, normalizedEntryValue];
216+},
217+);
218+if (entriesChanged) {
219+nextSchema[key] = Object.fromEntries(normalizedEntries);
220+changed = true;
221+}
222+}
223+224+return changed ? nextSchema : schema;
225+}
226+147227export function normalizeToolParameterSchema(
148228schema: unknown,
149229options?: { modelProvider?: string; modelId?: string; modelCompat?: ModelCompatConfig },
@@ -169,13 +249,14 @@ export function normalizeToolParameterSchema(
169249const unsupportedToolSchemaKeywords = resolveUnsupportedToolSchemaKeywords(options?.modelCompat);
170250171251function applyProviderCleaning(s: unknown): TSchema {
252+const normalizedSchema = normalizeArraySchemasMissingItems(s);
172253if (isGeminiProvider && !isAnthropicProvider) {
173-return cleanSchemaForGemini(s);
254+return cleanSchemaForGemini(normalizedSchema);
174255}
175256if (unsupportedToolSchemaKeywords.size > 0) {
176-return stripUnsupportedSchemaKeywords(s, unsupportedToolSchemaKeywords) as TSchema;
257+return stripUnsupportedSchemaKeywords(normalizedSchema, unsupportedToolSchemaKeywords) as TSchema;
177258}
178-return s as TSchema;
259+return normalizedSchema as TSchema;
179260}
180261181262const conditionalKey = getTopLevelConditionalKey(schemaRecord);
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。