* Applies structured output configuration to a payload based on model capabilities. * Uses json_schema with require_parameters for supported models, falls back to json_object with prompt instructions.
( targetPayload: any, messages: any[], responseFormat: any, model: string )
| 39 | * Uses json_schema with require_parameters for supported models, falls back to json_object with prompt instructions. |
| 40 | */ |
| 41 | async function applyResponseFormat( |
| 42 | targetPayload: any, |
| 43 | messages: any[], |
| 44 | responseFormat: any, |
| 45 | model: string |
| 46 | ): Promise<any[]> { |
| 47 | const useNative = await supportsNativeStructuredOutputs(model) |
| 48 | |
| 49 | if (useNative) { |
| 50 | logger.info('Using native structured outputs for OpenRouter model', { model }) |
| 51 | targetPayload.response_format = { |
| 52 | type: 'json_schema', |
| 53 | json_schema: { |
| 54 | name: responseFormat.name || 'response_schema', |
| 55 | schema: responseFormat.schema || responseFormat, |
| 56 | strict: responseFormat.strict !== false, |
| 57 | }, |
| 58 | } |
| 59 | targetPayload.provider = { ...targetPayload.provider, require_parameters: true } |
| 60 | return messages |
| 61 | } |
| 62 | |
| 63 | logger.info('Using json_object mode with prompt instructions for OpenRouter model', { model }) |
| 64 | const schema = responseFormat.schema || responseFormat |
| 65 | const schemaInstructions = generateSchemaInstructions(schema, responseFormat.name) |
| 66 | targetPayload.response_format = { type: 'json_object' } |
| 67 | return [...messages, { role: 'user', content: schemaInstructions }] |
| 68 | } |
| 69 | |
| 70 | export const openRouterProvider: ProviderConfig = { |
| 71 | id: 'openrouter', |
no test coverage detected