Skip to content

Commit e77074f

Browse files
committed
fix(translate): use json_object for custom API providers like DeepSeek
1 parent b7e8c92 commit e77074f

File tree

2 files changed

+25
-16
lines changed

2 files changed

+25
-16
lines changed

scripts/constants.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,9 @@ export const DEFAULT_OPENAI_MODEL = process.env.OPENAI_MODEL || "gpt-5-mini";
7474
export const DEFAULT_OPENAI_TEMPERATURE = 0.3;
7575
export const DEFAULT_OPENAI_MAX_TOKENS = 4096;
7676

77+
// Check if using OpenAI's default API (vs custom endpoint like DeepSeek)
78+
export const IS_CUSTOM_OPENAI_API = !!OPENAI_BASE_URL;
79+
7780
/**
7881
* GPT-5.2 supports custom temperature ONLY when reasoning_effort="none"
7982
* Based on: https://platform.openai.com/docs/guides/reasoning

scripts/notion-translate/translateFrontMatter.ts

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
TRANSLATION_RETRY_BASE_DELAY_MS,
1212
TRANSLATION_CHUNK_MAX_CHARS,
1313
OPENAI_BASE_URL,
14+
IS_CUSTOM_OPENAI_API,
1415
} from "../constants.js";
1516

1617
// Load environment variables
@@ -467,6 +468,26 @@ async function translateTextSingleCall(
467468
// For GPT-5.2, use reasoning_effort="none" to allow custom temperature
468469
const modelParams = getModelParams(model, { useReasoningNone: true });
469470

471+
// Use json_schema for OpenAI (strict), json_object for custom APIs (like DeepSeek)
472+
const responseFormat = IS_CUSTOM_OPENAI_API
473+
? { type: "json_object" as const }
474+
: {
475+
type: "json_schema" as const,
476+
json_schema: {
477+
name: "translation",
478+
schema: {
479+
type: "object",
480+
properties: {
481+
markdown: { type: "string" },
482+
title: { type: "string" },
483+
},
484+
required: ["markdown", "title"],
485+
additionalProperties: false,
486+
},
487+
strict: true,
488+
},
489+
};
490+
470491
for (let attempt = 0; attempt < MAX_RETRIES; attempt++) {
471492
try {
472493
const response = await openai.chat.completions.create({
@@ -475,22 +496,7 @@ async function translateTextSingleCall(
475496
{ role: "system", content: prompt },
476497
{ role: "user", content: textWithTitle },
477498
],
478-
response_format: {
479-
type: "json_schema",
480-
json_schema: {
481-
name: "translation",
482-
schema: {
483-
type: "object",
484-
properties: {
485-
markdown: { type: "string" },
486-
title: { type: "string" },
487-
},
488-
required: ["markdown", "title"],
489-
additionalProperties: false,
490-
},
491-
strict: true,
492-
},
493-
},
499+
response_format: responseFormat,
494500
...modelParams,
495501
});
496502

0 commit comments

Comments
 (0)