fix(translator): guard rate_limit_params before .get() in do_llm_translate

Closed
MO
20h ago
+1-1

Description

OpenAITranslator.do_llm_translate dereferences rate_limit_params.get(...) whenever enable_json_mode_if_requested is set, but llm_translate() forwards its rate_limit_params with a default of None:

def llm_translate(self, text, ignore_cache=False, rate_limit_params: dict = None):
    ...
    translation = self.do_llm_translate(text, rate_limit_params)  # may be None

A caller that does not supply rate_limit_params (e.g. a custom translator subclass built on OpenAITranslator with json mode enabled) crashes with AttributeError: 'NoneType' object has no attribute 'get'.

The fix adds the same truthiness guard already used in babeldoc/tools/executor/translator.py.

Verified: None, {}, {"request_json_mode": False} and {"request_json_mode": True} all evaluate without error.


Summary by cubic

Fixes a crash in OpenAITranslator.do_llm_translate when rate_limit_params is None and JSON mode is enabled.

  • Adds a None guard before calling .get() on rate_limit_params, matching the existing guard in translator.py.
  • Callers that omit rate_limit_params (e.g., custom translator subclasses) no longer raise AttributeError.