feat: Add structured output support for OpenAI to Gemini conversion

- Add support for response_format parameter in OpenAI requests
- Map json_object type to Gemini's responseMimeType: application/json
- Map json_schema type to Gemini's responseSchema with schema definition
- Update buildGeminiGenerationConfig to handle response_format parameter

This enables users to request structured JSON outputs using OpenAI's
response_format API when calling Gemini models through the converter.
This commit is contained in:
lethanhson9901 2025-12-09 11:08:35 +07:00
parent abf32f6cb5
commit 9701d98c55

View file

@ -711,13 +711,25 @@ export class OpenAIConverter extends BaseConverter {
/** /**
* 构建Gemini生成配置 * 构建Gemini生成配置
*/ */
buildGeminiGenerationConfig({ temperature, max_tokens, top_p, stop, tools }, model) { buildGeminiGenerationConfig({ temperature, max_tokens, top_p, stop, tools, response_format }, model) {
const config = {}; const config = {};
config.temperature = checkAndAssignOrDefault(temperature, 1); config.temperature = checkAndAssignOrDefault(temperature, 1);
config.maxOutputTokens = checkAndAssignOrDefault(max_tokens, 65535); config.maxOutputTokens = checkAndAssignOrDefault(max_tokens, 65535);
config.topP = checkAndAssignOrDefault(top_p, 0.95); config.topP = checkAndAssignOrDefault(top_p, 0.95);
if (stop !== undefined) config.stopSequences = Array.isArray(stop) ? stop : [stop]; if (stop !== undefined) config.stopSequences = Array.isArray(stop) ? stop : [stop];
// Handle response_format
if (response_format) {
if (response_format.type === 'json_object') {
config.responseMimeType = 'application/json';
} else if (response_format.type === 'json_schema' && response_format.json_schema) {
config.responseMimeType = 'application/json';
if (response_format.json_schema.schema) {
config.responseSchema = response_format.json_schema.schema;
}
}
}
// Gemini 2.5 and thinking models require responseModalities: ["TEXT"] // Gemini 2.5 and thinking models require responseModalities: ["TEXT"]
// But this parameter cannot be added when using tools (causes 400 error) // But this parameter cannot be added when using tools (causes 400 error)
const hasTools = tools && Array.isArray(tools) && tools.length > 0; const hasTools = tools && Array.isArray(tools) && tools.length > 0;