Bug: Spanish responses returned in single-message mode when English is selected
Summary
When the application is configured for single-message mode (CONTEXT_OPTION=single-message), the LLM (Gemma) responds in Spanish even when the user has selected English as their language. Switching to conversation-with-context-labels mode resolves the issue.
Root Cause
The language preference was lost at two points in the single-message request chain:
1. Frontend does not send language in single-message payload
File: components/gov-chat-frontend/src/components/ChatBotComponent.vue (lines 783-791)
The single-message payload omitted the context object entirely:
queryData = {
userId: ...,
sessionId: ...,
text: messageForBackend,
contextOption: contextOption,
timestamp: ...,
};
While conversation-with-labels mode explicitly includes language (lines 756-780):
context: {
categoryLabel: categoryLabel,
serviceLabels: serviceLabels,
language: this.currentLocale.toUpperCase(), // e.g. "EN"
}
this.currentLocale is always available (initialized to "en" at line 447) but was never included in the single-message payload.
2. Backend strips all context when building OPEA payload for single-message mode
File: components/gov-chat-backend/services/query-service.js (lines 406-409)
if (backendMode === 'single-message') {
opeaPayload = {
messages: queryText,
stream: false
// context: completely omitted
};
}
Even if the frontend were fixed, the backend unconditionally discarded all context (including language) for single-message mode. Conversation mode includes it (lines 410-421).
3. ChatQnA service crashes on string-type messages (discovered during testing)
File: genie-ai-overlay/chatqna/genieai_chatqna.py
In single-message mode, messages is sent as a plain string (the extracted last message text), not an array of message objects. The ChatQnA Python code had three locations that assumed full_chat_history was always a list of dicts:
-
_get_translated_history_string()(line 1090): iterated withmessage["content"]— crashed withTypeError: string indices must be integers -
Language auto-detection fallback (line 1285): iterated with
msg.get("role")— same crash -
English history flattening (line 1327): iterated with
msg.get('role')— same crash
Resolution
Fix 1: Frontend — Include language in single-message payload
File: components/gov-chat-frontend/src/components/ChatBotComponent.vue
Added context.language to the single-message queryData object:
queryData = {
userId: ...,
sessionId: ...,
text: messageForBackend,
context: {
language: this.currentLocale.toUpperCase(),
},
contextOption: contextOption,
timestamp: ...,
};
Fix 2: Backend — Pass context to OPEA in single-message mode
File: components/gov-chat-backend/services/query-service.js
Include context.language in the single-message OPEA payload:
if (backendMode === 'single-message') {
opeaPayload = {
messages: queryText,
stream: false,
context: {
language: queryData.context?.language,
},
};
}
Fix 3: ChatQnA — Handle string-type messages in single-message mode
File: genie-ai-overlay/chatqna/genieai_chatqna.py
Three locations updated to handle full_chat_history being either a str or List[Dict]:
-
_get_translated_history_string(): string input is translated directly via_translate_text_chunk()instead of iterating - Language auto-detection fallback: string input is used directly as
last_user_content - English history flattening: string input is used as-is
Affected Files
components/gov-chat-frontend/src/components/ChatBotComponent.vuecomponents/gov-chat-backend/services/query-service.jsgenie-ai-overlay/chatqna/genieai_chatqna.py
Testing
Smoke-tested on local build (RTX 4060, Gemma 3 1B) with CONTEXT_OPTION=single-message:
-
English quickhelp: LLM responded in English,
MANDATORY: respond ONLY in Englishguard injected correctly - Spanish quickhelp: Query translated to English, response translated back to Spanish
- Indonesian quickhelp: Query translated to English, response translated back to Indonesian
- Conversation-with-labels mode: No regressions observed
Fixed In
Branch: sprint-21-bug-fixes (commit ec2777cd)