Skip to content

fix(chat): answer in the question's language, or the one asked for, and rewrite an English answer once - #2062

Merged
gkhngyk merged 6 commits into
ansvisor:mainfrom
cihan-gyk:fix/chat-answer-language-haiku
Nov 3, 2026
Merged

gkhngyk merged 6 commits into
ansvisor:mainfrom
cihan-gyk:fix/chat-answer-language-haiku

Conversation

@cihan-gyk

Copy link
Copy Markdown
Collaborator

Summary

Chat answers in the language the person writes in, or in the language they ask for ("answer in English", "İngilizce cevap ver"). This is the owner's rule (2026-10-31, clarified 2026-11-03).

Chat's model test set (W4-35) caught a case that broke it: Claude Haiku 5.5 answered "Bu hafta Perplexity'de neden düştük?" in English. Haiku is the model that answers when it is picked in the menu, and the one that writes Help answers.

Why it happened: the language rule sits at the top of the system prompt. The writer then reads the person's question followed by pages of English data and app notes: read results, the injection note. A small model answers in the language it read last.

Fix (web/src/lib/chat/language.ts, used by engine.ts):

  1. The question's language, named last.
    • The writer's turn ends with one line: "The person wrote in Turkish: answer in Turkish, whatever the language of the data, unless they ask for another language."
    • When the person asks for a language, the line says to answer in that one instead.
    • The language is read from common words and letters (English, Turkish, German, French, Spanish, Italian, Portuguese, Dutch). When it is unclear, no line is added.
    • This line alone fixed the test case: with it, Haiku answered the same Turkish question in Turkish.
  2. One rewrite as a safety net.
    • If an answer still comes out in English for a question in another language, and the person didn't ask for English, the engine rewrites it once in the question's language. The person sees the rewrite replace the streamed text, through a new text.replaced event.
    • The rewrite runs on the same model, told to translate the words only and keep the Markdown, numbers, names, links and [placeholders].
    • It goes through the same id mask and fact check as streamed text.
    • It is held and charged on the AI budget like any step.
    • If it fails, or comes back still English, the answer stays as it was.
    • A question that asks for a language is never rewritten. "İngilizce bir blog yazısı yaz" stays English.

What the rewrite costs: only when it fires, which should be rare now that the language is named. It is one call: the answer goes in (about 200–1,500 tokens with the short instruction) and comes back about the same length. That is about $0.001 on Haiku 5.5, about $0.01–0.03 on Sonnet 5.5, under $0.001 on GPT-6 Luna, and about $0.01–0.03 on GPT-6.1 Sol or Gemini 4 Argon. It is charged to the workspace's AI limit like any step. An answer in the right language costs nothing extra; the language line is a few tokens.

Related issue

None. Found by the Chat Wave 4 model test set (W4-35).

Type of change

  • feat — New feature
  • fix — Bug fix
  • chore — Maintenance / dependencies
  • docs — Documentation only
  • refactor — Code change that neither fixes a bug nor adds a feature
  • test — Adding or updating tests

How to test

  1. cd web && yarn vitest run src/lib/chat/language.test.ts src/lib/chat/events.test.ts src/lib/chat/engine.test.ts -t "language". Covered:
    • a Turkish question gets a Turkish answer, with the language named last;
    • an English answer to a Turkish question is rewritten once, on the budget;
    • a Turkish question that says "Answer in English" gets English, with no rewrite;
    • "İngilizce bir blog yazısı yaz" stays English;
    • a rewrite that is still English is not kept.
  2. In Chat, pick Haiku 5.5 and ask "Bu hafta Perplexity'de neden düştük?": the answer is Turkish. Ask "Bu hafta neden düştük? Answer in English.": the answer is English.

No migration. No new environment variables. One new event type (text.replaced); an older browser skips it and keeps the streamed text. No Help change: the visible behavior is the rule Chat already promised.

Checklist

  • Branch follows the naming convention
  • Commits follow Conventional Commits
  • yarn lint passes
  • yarn typecheck passes
  • yarn format:check passes
  • Changes are focused — one concern per PR
  • Help article updated under help/ (or no user-visible change)

CI passed on this branch (f403e97), run on the fork (https://github.com/cihan-gyk/ansvisor/actions/runs/41599163691): schema is up to date; web format:check, lint, typecheck, all tests and yarn build pass; server lint, format:check and all tests pass.

🤖 Generated with Claude Code

cihan-gyk and others added 5 commits November 3, 2026 09:56
…iter, and rewrite an English answer once

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…the writer, never rewriting it away

Co-Authored-By: Claude Opus 5.5 <[email protected]>
… and keep a stopped rewrite's hold

"How visible are we in German prompts?" named a language in passing but read
as a request, so the writer was told to answer in it and the rewrite was off.
"in <language>" now asks only after an answer verb (not before a noun), before
"please"/"only", at the start, or ending a message that isn't a question, and
the writer's line names the language asked for.

A Stop during the rewrite now keeps its budget hold, as a model step does, and
ends the run stopped instead of finishing it.
"İngilizce yazılmış promptlar", "Almanca sayfamız cevaplarında" and
"Almanca olarak kaç prompt" named a language in passing but read as an
ask, as did "a German version of the site" and "in Spanish, please"
mid-sentence. A Turkish ask now needs a request verb as a whole word
after the language (cevap ver, yaz, anlat, çevir), with no place or
owner word between; a version or translation is asked for only after a
verb like give or write; "please" counts at the end of the message.
gkhngyk
gkhngyk previously approved these changes Nov 3, 2026
# Conflicts:
#	web/src/lib/chat/engine.test.ts
#	web/src/lib/chat/engine.ts

@gkhngyk gkhngyk left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants