-
Notifications
You must be signed in to change notification settings - Fork 395
feat(kosong): support structured response formats #1397
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "@moonshot-ai/kosong": patch | ||
| --- | ||
|
|
||
| Add provider-level structured response format support. | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,6 +10,7 @@ import type { | |
| FinishReason, | ||
| GenerateOptions, | ||
| ProviderRequestAuth, | ||
| ResponseFormat, | ||
| StreamedMessage, | ||
| ThinkingEffort, | ||
| } from '#/provider'; | ||
|
|
@@ -126,6 +127,19 @@ function toolToGoogleGenAI(tool: Tool): GoogleTool { | |
| ], | ||
| }; | ||
| } | ||
|
|
||
| function applyResponseFormat( | ||
| config: Record<string, unknown>, | ||
| format: ResponseFormat | undefined, | ||
| ): void { | ||
| if (format === undefined) return; | ||
| config['responseMimeType'] = 'application/json'; | ||
| delete config['responseSchema']; | ||
| delete config['responseJsonSchema']; | ||
| if (format.type === 'json_schema') { | ||
| config['responseJsonSchema'] = format.jsonSchema.schema; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
If a Google provider was configured with the native Useful? React with 👍 / 👎. |
||
| } | ||
| } | ||
| interface GoogleContent { | ||
| role: string; | ||
| parts: GooglePart[]; | ||
|
|
@@ -804,6 +818,7 @@ export class GoogleGenAIChatProvider implements ChatProvider { | |
| systemInstruction: systemPrompt, | ||
| ...(tools.length > 0 ? { tools: tools.map((t) => toolToGoogleGenAI(t)) } : {}), | ||
| }; | ||
| applyResponseFormat(config, options?.responseFormat); | ||
|
|
||
| try { | ||
| const client = this._createClient(options?.auth); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,6 +11,7 @@ import type { | |
| FinishReason, | ||
| GenerateOptions, | ||
| ProviderRequestAuth, | ||
| ResponseFormat, | ||
| StreamedMessage, | ||
| ThinkingEffort, | ||
| } from '#/provider'; | ||
|
|
@@ -362,6 +363,22 @@ interface ResponseToolParam { | |
| parameters: Record<string, unknown>; | ||
| strict: boolean; | ||
| } | ||
|
|
||
| function responseFormatToResponsesText(format: ResponseFormat): Record<string, unknown> { | ||
| if (format.type === 'json_object') { | ||
| return { format: { type: 'json_object' } }; | ||
| } | ||
| return { | ||
| format: { | ||
| type: 'json_schema', | ||
| name: format.jsonSchema.name, | ||
| schema: format.jsonSchema.schema, | ||
| strict: format.jsonSchema.strict, | ||
| description: format.jsonSchema.description, | ||
| }, | ||
| }; | ||
| } | ||
|
|
||
| // The Responses API has no input type for video, and only mp3/wav audio can | ||
| // be inlined as input_file data. Degrade such parts to placeholder text so | ||
| // the model still learns an attachment existed instead of silently losing it. | ||
|
|
@@ -1074,6 +1091,9 @@ export class OpenAIResponsesChatProvider implements ChatProvider { | |
| if (systemPrompt) { | ||
| createParams['instructions'] = systemPrompt; | ||
| } | ||
| if (options?.responseFormat !== undefined) { | ||
| createParams['text'] = responseFormatToResponsesText(options.responseFormat); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
When a caller has already configured Responses-specific Useful? React with 👍 / 👎. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
When callers have already configured other OpenAI Responses Useful? React with 👍 / 👎. |
||
| } | ||
|
|
||
| if ( | ||
| !('responses' in client) || | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This changeset lists only the internal
@moonshot-ai/kosongpackage, but the repository's changeset rules require internal package source changes that enter the CLI bundle to manually list@moonshot-ai/kimi-code. As written, the private/internal package can be versioned without bumping the published CLI artifact, so this provider-layer behavior may not ship in the next CLI release/changelog.Useful? React with 👍 / 👎.