From 5d845cbc295196504db238746d06bcbbf85c8fbd Mon Sep 17 00:00:00 2001 From: dazedanon Date: Sun, 1 Mar 2026 10:49:44 -0600 Subject: [PATCH] Fix Claude schema --- util/translation.py | 33 ++++++++++++++++++++++----------- 1 file changed, 22 insertions(+), 11 deletions(-) diff --git a/util/translation.py b/util/translation.py index 9becbad..803a30e 100644 --- a/util/translation.py +++ b/util/translation.py @@ -694,24 +694,30 @@ def translateText(system, user, history, penalty, formatType, model, numLines=No msg.append({"role": "assistant", "content": history}) # Response Format - # Most providers (OpenAI, Claude, Gemini) support json_schema. - # Deepseek only supports json_object. - _json_object_only = model and "deepseek" in model.lower() + # - OpenAI: response_format with json_schema wrapper {name, strict, schema} + # - Deepseek: response_format with json_object (no schema support) + # - Claude: output_config.format via extra_body {type, schema} — no response_format + # - Gemini: response_format with json_schema (OpenAI compat) + _is_claude = model and any(x in model.lower() for x in ("claude", "sonnet", "haiku", "opus")) + _is_deepseek = model and "deepseek" in model.lower() + if formatType == "json" and numLines is not None: - if _json_object_only: + schema = createTranslationSchema(numLines) + if _is_claude: + responseFormat = {"type": "text"} # unused for Claude; controlled via extra_body + _claude_output_config = {"format": {"type": "json_schema", "schema": schema}} + elif _is_deepseek: responseFormat = {"type": "json_object"} + _claude_output_config = None else: - schema = createTranslationSchema(numLines) responseFormat = { "type": "json_schema", - "json_schema": { - "name": "translation_response", - "strict": True, - "schema": schema - } + "json_schema": {"name": "translation_response", "strict": True, "schema": schema} } + _claude_output_config = None else: responseFormat = {"type": "text"} + _claude_output_config = None # Content to TL - ensure user content is not empty if not user or not str(user).strip(): @@ -750,10 +756,15 @@ def translateText(system, user, history, penalty, formatType, model, numLines=No } } except (ValueError, TypeError): - # Ignore if the value is not a valid integer pass # frequency_penalty is not supported via the OpenAI compatibility layer for Gemini + elif _is_claude: + params["temperature"] = 0 + # Claude uses output_config.format instead of response_format + del params["response_format"] + if _claude_output_config: + params["extra_body"] = {"output_config": _claude_output_config} else: # Default to OpenAI behavior if "gpt-5" in model: params["reasoning_effort"] = "minimal"