Fix Translation API Authentication Error | Generated by AI
Analysis of the Workflow and Error Cause
Your GitHub Actions workflow for “Translate Recent Notes” looks well-structured overall—it’s checking out the repo, setting up Python, installing deps, running a translation script on the N most recent posts, and committing changes to _notes/. However, based on the logs you shared earlier (with the 401 errors during translation to languages like ar, de, fr, etc.), the issue is in the authentication for the translation API calls inside scripts/translation/update_lang_notes.py.
Root Cause
- The error
"No cookie auth credentials found"(HTTP 401) is specific to the OpenRouter API (or a Python client/library interacting with it, like LiteLLM or an unofficial SDK). This happens when the API request lacks proper authentication headers. - OpenRouter expects
Authorization: Bearer <your_openrouter_api_key>in requests. If the key isn’t passed correctly, some clients fall back to (or misinterpret as needing) cookie-based session auth, triggering this exact error. - In your workflow:
- You’re setting
OPENROUTER_API_KEY: $, which passes a secret value to the script’s environment. - But the script likely isn’t reading/using this env var correctly. Common mismatches:
- The script expects
OPENAI_API_KEY(for OpenAI-compatible endpoints like OpenRouter). - Or it’s using a library (e.g.,
openaiPython SDK) without setting the base URL tohttps://openrouter.ai/api/v1. - The secret
DEEPSEEK_API_KEYmight actually hold your OpenRouter API key (routed to DeepSeek/Grok models), but if it’s a direct DeepSeek key, it won’t work for OpenRouter.
- The script expects
- You’re setting
- From the logs, the script is using model
'x-ai/grok-4-fast'(Grok 4 via OpenRouter), and it’s processing front matter/titles successfully but failing on content translation per language. - This isn’t a GitHub Actions issue—it’s in the Python script’s API client setup. The workflow continues to the commit step (hence the
git config user.name "github-actions[bot]"log), but without translations, only English files get added.
Recommended Fixes
- Update Environment Variables in Workflow:
- Align with common OpenRouter setups (OpenAI-compatible). Change the
envblock in the “Translate posts” step to:env: GITHUB_TOKEN: $ OPENAI_API_KEY: $ # Rename var to what the script expects OPENAI_BASE_URL: https://openrouter.ai/api/v1 # Required for routing to OpenRouter - If
DEEPSEEK_API_KEYis your OpenRouter key, great. If it’s a direct DeepSeek key, create a new secretOPENROUTER_API_KEYin repo settings with your actual OpenRouter key (get one at openrouter.ai/keys). - Test: Add
echo $OPENAI_API_KEY(redacted) to the run step for debugging in logs.
- Align with common OpenRouter setups (OpenAI-compatible). Change the
- Fix the Python Script (
update_lang_notes.py):- Ensure it initializes the OpenAI client like this (assuming
openailibrary):import os from openai import OpenAI client = OpenAI( api_key=os.getenv("OPENAI_API_KEY"), base_url=os.getenv("OPENAI_BASE_URL", "https://api.openai.com/v1") # Defaults to OpenAI if not set ) # Then use client.chat.completions.create(..., model="x-ai/grok-4-fast") - If using LiteLLM (common for multi-provider): Install it if not in
requirements.txt, and callcompletion(model="openrouter/x-ai/grok-4-fast", api_key=os.getenv("OPENAI_API_KEY"), api_base="https://openrouter.ai/api/v1", ...). - For the translation loop: Add error handling per language (e.g.,
try/exceptaround API calls, log failures, skip if 401). - Handle cookies explicitly? Unlikely needed—stick to API keys. If the script has browser/Selenium code, remove it for CI reliability.
- Ensure it initializes the OpenAI client like this (assuming
- Workflow Improvements:
- Input Validation: Add a check for N=0 or invalid
--nto avoid processing nothing. - Conditional Commit: Your
git diff --cached --quiet || git commitis good, but update the message to reflect all languages:-m "Add translated posts (ar, de, es, fr, hi, ja, zh, hant)". - Caching: Add
actions/cacheforpipto speed up installs. - Debug Mode: Add an optional input
debug: trueand--debugflag to the script for verbose logging. - Permissions: Ensure the
github-pagesenvironment has read/write access if needed. - Full updated YAML snippet for the “Translate posts” step:
```
- name: Translate posts run: | python scripts/translation/update_lang_notes.py –n $ env: GITHUB_TOKEN: $ OPENAI_API_KEY: $ OPENAI_BASE_URL: https://openrouter.ai/api/v1 ```
- Input Validation: Add a check for N=0 or invalid
- Testing Steps:
- Local Test: Run
python scripts/translation/update_lang_notes.py --n 1locally with env vars exported (e.g.,export OPENAI_API_KEY=your_key). - Workflow Test: Trigger with
workflow_dispatchand N=1, check logs for API calls. - Verify Secret: In repo settings > Secrets, confirm
DEEPSEEK_API_KEYhas a valid OpenRouter key (starts withsk-or-...). - If still failing, add
--verboseto the script and inspect request headers (e.g., viaprint(client.api_key)).
- Local Test: Run
This should resolve the 401s and get translations flowing. If you share snippets from update_lang_notes.py (e.g., the API client init), I can pinpoint more precisely.
References
- OpenRouter Quickstart (API Auth Setup)
- LiteLLM OpenRouter Integration (if using)
- GitHub Issue on Similar OpenRouter Auth Error
- Reddit Thread on OpenRouter 401 Errors