fix(ai): sanitize Mistral tool schemas closes #3361
This commit is contained in:
@@ -2,6 +2,10 @@
|
||||
|
||||
## [Unreleased]
|
||||
|
||||
### Fixed
|
||||
|
||||
- Fixed direct Mistral tool definitions to strip TypeBox symbol metadata before passing schemas to the SDK, restoring tool calls after the SDK's stricter outbound validation ([#3361](https://github.com/badlogic/pi-mono/issues/3361))
|
||||
|
||||
## [0.67.67] - 2026-04-17
|
||||
|
||||
### Added
|
||||
|
||||
@@ -456,12 +456,28 @@ function toFunctionTools(tools: Tool[]): Array<FunctionTool & { type: "function"
|
||||
function: {
|
||||
name: tool.name,
|
||||
description: tool.description,
|
||||
parameters: tool.parameters as unknown as Record<string, unknown>,
|
||||
parameters: stripSymbolKeys(tool.parameters) as Record<string, unknown>,
|
||||
strict: false,
|
||||
},
|
||||
}));
|
||||
}
|
||||
|
||||
function stripSymbolKeys(value: unknown): unknown {
|
||||
if (Array.isArray(value)) {
|
||||
return value.map((item) => stripSymbolKeys(item));
|
||||
}
|
||||
|
||||
if (value && typeof value === "object") {
|
||||
const result: Record<string, unknown> = {};
|
||||
for (const [key, entry] of Object.entries(value)) {
|
||||
result[key] = stripSymbolKeys(entry);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
function toChatMessages(messages: Message[], supportsImages: boolean): ChatCompletionStreamRequestMessage[] {
|
||||
const result: ChatCompletionStreamRequestMessage[] = [];
|
||||
|
||||
|
||||
61
packages/ai/test/mistral-tool-schema.test.ts
Normal file
61
packages/ai/test/mistral-tool-schema.test.ts
Normal file
@@ -0,0 +1,61 @@
|
||||
import { Type } from "@sinclair/typebox";
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { getModel } from "../src/models.js";
|
||||
import { complete } from "../src/stream.js";
|
||||
import type { Context, Model } from "../src/types.js";
|
||||
|
||||
interface MistralToolPayload {
|
||||
tools?: Array<{
|
||||
type: "function";
|
||||
function: {
|
||||
name: string;
|
||||
parameters: Record<string, unknown>;
|
||||
};
|
||||
}>;
|
||||
}
|
||||
|
||||
describe("Mistral tool schema serialization", () => {
|
||||
it("strips TypeBox symbol keys before the SDK validates tool schemas", async () => {
|
||||
const model: Model<"mistral-conversations"> = {
|
||||
...getModel("mistral", "devstral-medium-latest"),
|
||||
baseUrl: "http://127.0.0.1:9",
|
||||
};
|
||||
const parameters = Type.Object({
|
||||
nested: Type.Object({
|
||||
value: Type.String(),
|
||||
}),
|
||||
});
|
||||
const context: Context = {
|
||||
messages: [{ role: "user", content: "Hi", timestamp: Date.now() }],
|
||||
tools: [
|
||||
{
|
||||
name: "inspect_schema",
|
||||
description: "Inspect the schema",
|
||||
parameters,
|
||||
},
|
||||
],
|
||||
};
|
||||
let capturedPayload: MistralToolPayload | undefined;
|
||||
|
||||
const response = await complete(model, context, {
|
||||
apiKey: "fake-key",
|
||||
onPayload: (payload) => {
|
||||
capturedPayload = payload as MistralToolPayload;
|
||||
return payload;
|
||||
},
|
||||
});
|
||||
|
||||
expect(capturedPayload?.tools).toHaveLength(1);
|
||||
const payloadParameters = capturedPayload?.tools?.[0]?.function.parameters;
|
||||
expect(payloadParameters).toBeDefined();
|
||||
expect(Object.getOwnPropertySymbols(payloadParameters ?? {})).toHaveLength(0);
|
||||
const properties = payloadParameters?.properties;
|
||||
expect(properties).toBeTruthy();
|
||||
expect(Object.getOwnPropertySymbols((properties as Record<string, unknown>) ?? {})).toHaveLength(0);
|
||||
const nested = (properties as Record<string, unknown> | undefined)?.nested;
|
||||
expect(nested).toBeTruthy();
|
||||
expect(Object.getOwnPropertySymbols((nested as Record<string, unknown>) ?? {})).toHaveLength(0);
|
||||
expect(response.stopReason).toBe("error");
|
||||
expect(response.errorMessage).not.toContain("Input validation failed");
|
||||
});
|
||||
});
|
||||
@@ -767,11 +767,12 @@ describe("Generate E2E Tests", () => {
|
||||
|
||||
it("should handle thinking mode", { retry: 3 }, async () => {
|
||||
const llm = getModel("mistral", "magistral-medium-latest");
|
||||
await handleThinking(llm, { reasoningEffort: "medium" });
|
||||
await handleThinking(llm, { promptMode: "reasoning" });
|
||||
});
|
||||
|
||||
it("should handle multi-turn with thinking and tools", { retry: 3 }, async () => {
|
||||
await multiTurn(llm, { reasoningEffort: "medium" });
|
||||
const llm = getModel("mistral", "magistral-medium-latest");
|
||||
await multiTurn(llm, { promptMode: "reasoning" });
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user