Skip to content

Commit 159a48a

Browse files
committed
fix(providers): scope Z.ai schema guidance to the response_format call only
- schemaGuidance now falls back to the bare responseFormat object when .schema is absent, matching the schema-or-format fallback used elsewhere in the codebase (was silently injecting nothing for callers that pass a bare JSON schema) - guidance is now only appended to the messages sent alongside an actual response_format (the immediate call, or whichever pass deferResponseFormat applies it to) instead of every turn of an active tool loop, where it wrongly told the model to return final JSON instead of continuing to call tools
1 parent 1f1a45a commit 159a48a

1 file changed

Lines changed: 30 additions & 9 deletions

File tree

apps/sim/providers/zai/index.ts

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,22 @@ const logger = createLogger('ZaiProvider')
2929

3030
const ZAI_BASE_URL = 'https://api.z.ai/api/paas/v4'
3131

32+
function buildSchemaGuidance(responseFormat: ProviderRequest['responseFormat']): string {
33+
if (!responseFormat) return ''
34+
const schema = responseFormat.schema || responseFormat
35+
return `\n\nYour response must be valid JSON matching this schema${
36+
responseFormat.name ? ` ("${responseFormat.name}")` : ''
37+
}:\n${JSON.stringify(schema, null, 2)}`
38+
}
39+
40+
function withSchemaGuidance(messages: any[], guidance: string): any[] {
41+
if (!guidance) return messages
42+
if (messages[0]?.role === 'system') {
43+
return [{ ...messages[0], content: `${messages[0].content}${guidance}` }, ...messages.slice(1)]
44+
}
45+
return [{ role: 'system', content: guidance.trimStart() }, ...messages]
46+
}
47+
3248
/**
3349
* Z.ai's GLM models via an OpenAI-compatible chat-completions API (`api.z.ai`), with these
3450
* documented deviations from a standard OpenAI-compatible adapter:
@@ -68,16 +84,10 @@ export const zaiProvider: ProviderConfig = {
6884

6985
const allMessages = []
7086

71-
const schemaGuidance = request.responseFormat
72-
? `\n\nYour response must be valid JSON matching this schema${
73-
request.responseFormat.name ? ` ("${request.responseFormat.name}")` : ''
74-
}:\n${JSON.stringify(request.responseFormat.schema, null, 2)}`
75-
: ''
76-
77-
if (request.systemPrompt || schemaGuidance) {
87+
if (request.systemPrompt) {
7888
allMessages.push({
7989
role: 'system',
80-
content: `${request.systemPrompt || ''}${schemaGuidance}`,
90+
content: request.systemPrompt,
8191
})
8292
}
8393

@@ -147,6 +157,10 @@ export const zaiProvider: ProviderConfig = {
147157
const deferResponseFormat = !!responseFormatPayload && hasActiveTools
148158
if (responseFormatPayload && !deferResponseFormat) {
149159
payload.response_format = responseFormatPayload
160+
payload.messages = withSchemaGuidance(
161+
payload.messages,
162+
buildSchemaGuidance(request.responseFormat)
163+
)
150164
}
151165

152166
if (request.stream && (!tools || tools.length === 0 || !hasActiveTools)) {
@@ -489,6 +503,10 @@ export const zaiProvider: ProviderConfig = {
489503
streamingPayload.tool_choice = undefined
490504
if (deferResponseFormat && responseFormatPayload) {
491505
streamingPayload.response_format = responseFormatPayload
506+
streamingPayload.messages = withSchemaGuidance(
507+
streamingPayload.messages,
508+
buildSchemaGuidance(request.responseFormat)
509+
)
492510
}
493511

494512
const streamResponse = await zai.chat.completions.create(
@@ -562,7 +580,10 @@ export const zaiProvider: ProviderConfig = {
562580
const finalFormatStartTime = Date.now()
563581
const finalPayload: any = {
564582
...payload,
565-
messages: currentMessages,
583+
messages: withSchemaGuidance(
584+
currentMessages,
585+
buildSchemaGuidance(request.responseFormat)
586+
),
566587
response_format: responseFormatPayload,
567588
}
568589
finalPayload.tools = undefined

0 commit comments

Comments
 (0)