Fix Translation API Authentication Error | Generated by AI

Home 2025.11

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

  1. Update Environment Variables in Workflow:
    • Align with common OpenRouter setups (OpenAI-compatible). Change the env block 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_KEY is your OpenRouter key, great. If it’s a direct DeepSeek key, create a new secret OPENROUTER_API_KEY in 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.
  2. Fix the Python Script (update_lang_notes.py):
    • Ensure it initializes the OpenAI client like this (assuming openai library):
      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 call completion(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/except around 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.
  3. Workflow Improvements:
    • Input Validation: Add a check for N=0 or invalid --n to avoid processing nothing.
    • Conditional Commit: Your git diff --cached --quiet || git commit is good, but update the message to reflect all languages: -m "Add translated posts (ar, de, es, fr, hi, ja, zh, hant)".
    • Caching: Add actions/cache for pip to speed up installs.
    • Debug Mode: Add an optional input debug: true and --debug flag to the script for verbose logging.
    • Permissions: Ensure the github-pages environment 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 ```
  4. Testing Steps:
    • Local Test: Run python scripts/translation/update_lang_notes.py --n 1 locally with env vars exported (e.g., export OPENAI_API_KEY=your_key).
    • Workflow Test: Trigger with workflow_dispatch and N=1, check logs for API calls.
    • Verify Secret: In repo settings > Secrets, confirm DEEPSEEK_API_KEY has a valid OpenRouter key (starts with sk-or-...).
    • If still failing, add --verbose to the script and inspect request headers (e.g., via print(client.api_key)).

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


Back

x-ai/grok-4-fast

Donate