Skip to content
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -846,6 +846,60 @@ describe('LoggingContentGenerator', () => {
expect(openaiError).toBeUndefined();
});

it('keeps the logged tool shape in step with the wire model', async () => {
Comment thread
yiliang114 marked this conversation as resolved.
Resolved
const wrapped = createWrappedGenerator(
vi
.fn()
.mockResolvedValue(
createResponse('resp-tool-shape', 'test-model', [{ text: 'ok' }]),
),
vi.fn(),
);
const generatorConfig = {
model: 'MiniMax-M2.5',
authType: AuthType.USE_OPENAI,
enableOpenAILogging: true,
openAILoggingDir: 'logs',
schemaCompliance: 'openapi_30' as const,
};
const generator = new LoggingContentGenerator(
wrapped,
createConfig({ authType: AuthType.USE_ANTHROPIC }),
generatorConfig,
);
const tools = [
{
functionDeclarations: [
{ name: 'cron_list', description: 'desc', parameters: {} },
],
},
];
const request = (model: string) =>
({
model,
contents: [{ role: 'user', parts: [{ text: 'Hello' }] }],
config: { tools },
}) as unknown as GenerateContentParameters;

// The request decides which backend answers, so the logged body has to
// follow the wire model rather than the configured one (#11834).
await generator.generateContent(request('llama-3.1-8b-instruct'), 'p-1');
await generator.generateContent(request('MiniMax-M3'), 'p-2');

expect(convertLlmToolsToOpenAISpy).toHaveBeenNthCalledWith(
1,
tools,
'openapi_30',
{ keepParameterlessParameters: false },
);
expect(convertLlmToolsToOpenAISpy).toHaveBeenNthCalledWith(
2,
tools,
'openapi_30',
{ keepParameterlessParameters: true },
);
});

it('omits request_text and response_text from API telemetry when logPrompts is false', async () => {
const wrapped = createWrappedGenerator(
vi
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import type {
InputModalities,
} from '../contentGenerator.js';
import { OpenAIContentConverter } from '../openaiContentGenerator/converter.js';
import { MiniMaxOpenAICompatibleProvider } from '../openaiContentGenerator/provider/minimax.js';
import { openaiRequestCaptureContext } from '../openaiContentGenerator/requestCaptureContext.js';
import type { RequestContext } from '../openaiContentGenerator/types.js';
import { OpenAILogger } from '../../utils/openaiLogger.js';
Expand Down Expand Up @@ -1045,6 +1046,17 @@ export class LoggingContentGenerator implements ContentGenerator {
await OpenAIContentConverter.convertLlmToolsToOpenAI(
request.config.tools,
this.schemaCompliance ?? 'auto',
{
// Mirror the pipeline's serialization (see pipeline.ts) so the
// logged body is the body actually sent — the MiniMax routing
// keeps `parameters` on zero-argument tools (#11834). Pass the
// request-level model so a model override logs what it sends.
keepParameterlessParameters:
Comment thread
yiliang114 marked this conversation as resolved.
Resolved
MiniMaxOpenAICompatibleProvider.isMiniMaxRouting(
this.generatorConfig,
request.model,
),
},
);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
/**
* @license
* Copyright 2026 Qwen Team
* SPDX-License-Identifier: Apache-2.0
*/

import { describe, expect, it } from 'vitest';
import type { Tool } from '@google/genai';
import type { Config } from '../../config/config.js';
import { ListAgentsTool } from '../../tools/list-agents.js';
import { convertLlmToolsToOpenAI } from './converter.js';

/**
* A zero-argument tool's `parameters` key is contested on the wire:
*
* - llama.cpp cannot compile a grammar over an empty `properties` map (#10080)
* and strict OpenAI-contract validators such as LM Studio reject
* `parameters: {"type":"object"}` without `properties` (#11410), which is why
* #11431 made omitting the key the default;
* - MiniMax rejects the omission itself — a request carrying the
* always-registered `list_agents` answers `400 invalid params, function
* parameters is empty (2013)` before the model is even reached (#11834).
*
* There is no single shape both accept, so the omission stays the default and
* the MiniMax routing opts out. Every assertion here is made on the
* *serialized* body: the defect is `JSON.stringify` dropping the key, so an
* assertion on the returned object could pass while the wire still omits it.
*/
describe('convertLlmToolsToOpenAI parameterless `parameters` (#11834)', () => {
/**
* The declaration from the report, taken from the real tool rather than a
* hand-written copy of its schema. `list_agents` is registered
* unconditionally, so every default interactive request carries it — which
* is why a bare greeting fails with no tool call in the conversation yet.
*/
function listAgentsTools(): Tool[] {
const declaration = new ListAgentsTool({} as Config).schema;
return [{ functionDeclarations: [declaration] }] as Tool[];
}

function wire(tools: unknown): string {
return JSON.stringify(tools);
}

it('keeps an explicit empty `parameters` object when the routing opts out', async () => {
const result = await convertLlmToolsToOpenAI(listAgentsTools(), 'auto', {
keepParameterlessParameters: true,
});

expect(result).toHaveLength(1);
expect(result[0].function.name).toBe('list_agents');
expect(wire(result)).toContain(
'"parameters":{"type":"object","properties":{}}',
);
expect(result[0].function.parameters).toEqual({
type: 'object',
properties: {},
});
});

it('still omits `parameters` by default', async () => {
const result = await convertLlmToolsToOpenAI(listAgentsTools());

expect(wire(result)).not.toContain('parameters');
expect(result[0].function.parameters).toBeUndefined();
});

it('still omits `parameters` when the opt-out is explicitly disabled', async () => {
const result = await convertLlmToolsToOpenAI(listAgentsTools(), 'auto', {
keepParameterlessParameters: false,
});

expect(wire(result)).not.toContain('parameters');
expect(result[0].function.parameters).toBeUndefined();
});

it('gates an MCP-style zero-argument tool the same way', async () => {
// An MCP server that declares no input schema reaches the converter as
// this default shape.
const tools = [
{
functionDeclarations: [
{
name: 'mcp_ping',
description: 'No arguments',
parametersJsonSchema: { type: 'object', properties: {} },
},
],
},
] as Tool[];

const kept = await convertLlmToolsToOpenAI(tools, 'auto', {
keepParameterlessParameters: true,
});
expect(wire(kept)).toContain(
'"parameters":{"type":"object","properties":{}}',
);

const omitted = await convertLlmToolsToOpenAI(tools);
expect(wire(omitted)).not.toContain('parameters');
});

it('leaves a tool that declares arguments identical on both paths', async () => {
const tools = [
{
functionDeclarations: [
{
name: 'read_file',
description: 'Reads a file',
parametersJsonSchema: {
type: 'object',
properties: { path: { type: 'string' } },
required: ['path'],
additionalProperties: false,
},
},
],
},
] as Tool[];

const kept = await convertLlmToolsToOpenAI(tools, 'auto', {
keepParameterlessParameters: true,
});
const omitted = await convertLlmToolsToOpenAI(tools);

expect(kept).toEqual(omitted);
expect(wire(kept)).toContain('"path"');
});

it('does not synthesise `parameters` for a declaration that never carried a schema', async () => {
// No registration path produces this — tools always expose
// `parametersJsonSchema`, and an MCP tool without an input schema gets an
// empty object one — so the opt-out deliberately stops at undoing the
// omission #11431 introduced.
const tools = [
{
functionDeclarations: [{ name: 'bare', description: 'No schema' }],
},
] as Tool[];

const result = await convertLlmToolsToOpenAI(tools, 'auto', {
keepParameterlessParameters: true,
});

expect(result[0].function.parameters).toBeUndefined();
expect(wire(result)).not.toContain('parameters');
});
});
15 changes: 14 additions & 1 deletion packages/core/src/core/openaiContentGenerator/converter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,7 @@ function isStrictlyValidSchema(schema: object): boolean {
export async function convertLlmToolsToOpenAI(
llmTools: ToolListUnion,
schemaCompliance: SchemaComplianceMode = 'auto',
options: { keepParameterlessParameters?: boolean } = {},
): Promise<OpenAI.Chat.ChatCompletionTool[]> {
const openAITools: OpenAI.Chat.ChatCompletionTool[] = [];

Expand Down Expand Up @@ -447,7 +448,19 @@ export async function convertLlmToolsToOpenAI(
PARAMETERLESS_SCHEMA_KEYS.has(key),
)
) {
parameters = undefined;
// Dropping the key is the default because llama.cpp cannot
// compile a grammar over an empty `properties` map (#10080) and
// strict OpenAI-contract validators such as LM Studio reject
// `parameters: {"type":"object"}` without `properties` (#11410).
// MiniMax rejects the omission instead — every request carrying
// the always-registered `list_agents` answers `400 invalid
// params, function parameters is empty (2013)` (#11834) — so
// that routing opts back into an explicit empty object schema,
// the shape the Anthropic wire substitutes for a missing
// `inputSchema`.
parameters = options.keepParameterlessParameters
? { type: 'object', properties: {} }
: undefined;
}
}

Expand Down
105 changes: 105 additions & 0 deletions packages/core/src/core/openaiContentGenerator/pipeline.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -605,6 +605,7 @@ describe('ContentGenerationPipeline', () => {
expect(mockConverter.convertLlmToolsToOpenAI).toHaveBeenCalledWith(
request.config!.tools,
'auto',
{ keepParameterlessParameters: false },
);
expect(mockClient.chat.completions.create).toHaveBeenCalledWith(
expect.objectContaining({
Expand All @@ -616,6 +617,110 @@ describe('ContentGenerationPipeline', () => {
);
});

it('keeps an empty parameters object when the wire model is MiniMax', async () => {
// Arrange — MiniMax rejects a function declaration with no
// `parameters` (#11834), and this route only reaches it by model id.
const request: GenerateContentParameters = {
model: 'MiniMax-M3',
contents: [{ parts: [{ text: 'Hello' }], role: 'user' }],
config: {
tools: [
{
functionDeclarations: [
{
name: 'test-function',
description: 'Test function',
parameters: { type: Type.OBJECT, properties: {} },
},
],
},
],
},
};

(mockConverter.convertLlmRequestToOpenAI as Mock).mockReturnValue([
{ role: 'user', content: 'Hello' },
] as OpenAI.Chat.ChatCompletionMessageParam[]);
(mockConverter.convertLlmToolsToOpenAI as Mock).mockResolvedValue([
{ type: 'function', function: { name: 'test-function' } },
] as OpenAI.Chat.ChatCompletionTool[]);
(mockConverter.convertOpenAIResponseToLlm as Mock).mockReturnValue(
new GenerateContentResponse(),
);
(mockClient.chat.completions.create as Mock).mockResolvedValue({
id: 'response-id',
choices: [
{ message: { content: 'Hello response' }, finish_reason: 'stop' },
],
} as OpenAI.Chat.ChatCompletion);

// Act
await pipeline.execute(request, 'test-prompt-id');

// Assert
expect(mockConverter.convertLlmToolsToOpenAI).toHaveBeenCalledWith(
request.config!.tools,
'auto',
{ keepParameterlessParameters: true },
);
});

it('gates on the request model override rather than the config model', async () => {
// Arrange — a request-level override decides which backend answers, so
// a MiniMax config model answering a non-MiniMax request must not flip
// the shape (and vice versa; see the case above).
const overridePipeline = new ContentGenerationPipeline({
...mockConfig,
contentGeneratorConfig: {
...mockContentGeneratorConfig,
model: 'MiniMax-M2.5',
},
});
const request: GenerateContentParameters = {
model: 'llama-3.1-8b-instruct',
contents: [{ parts: [{ text: 'Hello' }], role: 'user' }],
config: {
tools: [
{
functionDeclarations: [
{
name: 'test-function',
description: 'Test function',
parameters: { type: Type.OBJECT, properties: {} },
},
],
},
],
},
};

(mockConverter.convertLlmRequestToOpenAI as Mock).mockReturnValue([
{ role: 'user', content: 'Hello' },
] as OpenAI.Chat.ChatCompletionMessageParam[]);
(mockConverter.convertLlmToolsToOpenAI as Mock).mockResolvedValue([
{ type: 'function', function: { name: 'test-function' } },
] as OpenAI.Chat.ChatCompletionTool[]);
(mockConverter.convertOpenAIResponseToLlm as Mock).mockReturnValue(
new GenerateContentResponse(),
);
(mockClient.chat.completions.create as Mock).mockResolvedValue({
id: 'response-id',
choices: [
{ message: { content: 'Hello response' }, finish_reason: 'stop' },
],
} as OpenAI.Chat.ChatCompletion);

// Act
await overridePipeline.execute(request, 'test-prompt-id');

// Assert
expect(mockConverter.convertLlmToolsToOpenAI).toHaveBeenCalledWith(
request.config!.tools,
'auto',
{ keepParameterlessParameters: false },
);
});

it('should skip empty tools array in request', async () => {
// Arrange — tools: [] should NOT be included in the API request
const request: GenerateContentParameters = {
Expand Down
Loading
Loading