Skip to content

Fix OpenAI-compatible translators by omitting empty stop/max_tokens params#1111

Merged
reycn merged 1 commit into
PDFMathTranslate:mainfrom
mengdehong:fix/openai-compatible-empty-params
Apr 6, 2026
Merged

Fix OpenAI-compatible translators by omitting empty stop/max_tokens params#1111
reycn merged 1 commit into
PDFMathTranslate:mainfrom
mengdehong:fix/openai-compatible-empty-params

Conversation

@mengdehong
Copy link
Copy Markdown
Contributor

Problem and Reproduction

Gemini requests sent through the shared OpenAITranslator path included empty optional params:

  • stop: []
  • max_tokens: null

This caused Gemini's OpenAI-compatible endpoint to reject requests with:

Traceback (most recent call last):
    File "<string>", line 1, in <module>
    File "/app/pdf2zh/translator.py", line 100, in translate
      translation = self.do_translate(text)
                    ^^^^^^^^^^^^^^^^^^^^^^^
    File "/usr/local/lib/python3.12/site-packages/tenacity/__init__.py",
  line 331, in wrapped_f
      return copy(f, *args, **kw)
             ^^^^^^^^^^^^^^^^^^^^
    File "/usr/local/lib/python3.12/site-packages/tenacity/__init__.py",
  line 470, in __call__
      do = self.iter(retry_state=retry_state)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "/usr/local/lib/python3.12/site-packages/tenacity/__init__.py",
  line 371, in iter
      result = action(retry_state)
               ^^^^^^^^^^^^^^^^^^^
    File "/usr/local/lib/python3.12/site-packages/tenacity/__init__.py",
  line 393, in <lambda>
      self._add_action_func(lambda rs: rs.outcome.result())
                                       ^^^^^^^^^^^^^^^^^^^
    File "/usr/local/lib/python3.12/concurrent/futures/_base.py", line
  449, in result
      return self.__get_result()
             ^^^^^^^^^^^^^^^^^^^
    File "/usr/local/lib/python3.12/concurrent/futures/_base.py", line
  401, in __get_result
      raise self._exception
    File "/usr/local/lib/python3.12/site-packages/tenacity/__init__.py",
  line 473, in __call__
      result = fn(*args, **kwargs)
               ^^^^^^^^^^^^^^^^^^^
    File "/app/pdf2zh/translator.py", line 471, in do_translate
      response = self.client.chat.completions.create(
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "/usr/local/lib/python3.12/site-packages/openai/_utils/
  _utils.py", line 286, in wrapper
      return func(*args, **kwargs)
             ^^^^^^^^^^^^^^^^^^^^^
    File "/usr/local/lib/python3.12/site-packages/openai/resources/chat/
  completions/completions.py", line 1211, in create
      return self._post(
             ^^^^^^^^^^^
    File "/usr/local/lib/python3.12/site-packages/openai/
  _base_client.py", line 1297, in post
      return cast(ResponseT, self.request(cast_to, opts, stream=stream,
  stream_cls=stream_cls))

  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "/usr/local/lib/python3.12/site-packages/openai/
  _base_client.py", line 1070, in request
      raise self._make_status_error_from_response(err.response) from None
  openai.BadRequestError: Error code: 400 - [{'error': {'code': 400,
  'message': 'Value is not a string: []', 'status': 'INVALID_ARGUMENT'}}]

Reproduction:

  from pdf2zh.translator import GeminiTranslator

  t = GeminiTranslator("en", "zh", None, ignore_cache=True)
  print(t.translate("Hello world"))

Changes

  • Omit stop unless stop_tokens is non-empty
  • Omit max_tokens unless it is explicitly positive
  • Use .get(...) when recording optional cache-impact params to avoid
    KeyError

Reference

Copilot AI review requested due to automatic review settings March 31, 2026 06:03
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Fixes OpenAI-compatible translator requests (notably Gemini via OpenAITranslator) by omitting optional parameters when they are empty/unset, preventing 400 errors from strict OpenAI-compat endpoints.

Changes:

  • Only include stop in OpenAI chat completion options when stop_tokens is non-empty.
  • Only include max_tokens in options when it is a positive integer.
  • Avoid KeyError when recording cache-impacting params by using dict.get(...) for optional options.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread pdf2zh/translator.py
@reycn reycn merged commit 972c2df into PDFMathTranslate:main Apr 6, 2026
8 of 9 checks passed
@reycn
Copy link
Copy Markdown
Collaborator

reycn commented Apr 6, 2026

Merged, thanks for contributing @mengdehong !

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.

3 participants