#4432 KeyError: 'code'

2405:4802* Posted at: 2 hours ago 👁10

'code':Traceback (most recent call last):
File "/Users/nhu/Desktop/ProjectX/pyvideotrans/videotrans/translator/_base.py", line 98, in run

return self._run_text(split_source_text)

File "/Users/nhu/Desktop/ProjectX/pyvideotrans/videotrans/translator/_base.py", line 119, in _run_text

result = tools.cleartext(self._item_task(it))

File "/Users/nhu/Desktop/ProjectX/pyvideotrans/.venv/lib/python3.10/site-packages/tenacity/__init__.py", line 338, in wrapped_f

return copy(f, *args, **kw)

File "/Users/nhu/Desktop/ProjectX/pyvideotrans/.venv/lib/python3.10/site-packages/tenacity/__init__.py", line 477, in call

do = self.iter(retry_state=retry_state)

File "/Users/nhu/Desktop/ProjectX/pyvideotrans/.venv/lib/python3.10/site-packages/tenacity/__init__.py", line 378, in iter

result = action(retry_state)

File "/Users/nhu/Desktop/ProjectX/pyvideotrans/.venv/lib/python3.10/site-packages/tenacity/__init__.py", line 421, in exc_check

raise retry_exc from fut.exception()

tenacity.RetryError: RetryError[]

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/Users/nhu/Desktop/ProjectX/pyvideotrans/videotrans/util/TestSrtTrans.py", line 19, in run

text:List[dict] = translator.run(translate_type=self.translator_type,

File "/Users/nhu/Desktop/ProjectX/pyvideotrans/videotrans/translator/__init__.py", line 984, in run

return TransAPI(**kwargs).run()

File "/Users/nhu/Desktop/ProjectX/pyvideotrans/videotrans/translator/_base.py", line 100, in run

raise e.last_attempt.exception()

File "/Users/nhu/Desktop/ProjectX/pyvideotrans/.venv/lib/python3.10/site-packages/tenacity/__init__.py", line 480, in call

result = fn(*args, **kwargs)

File "/Users/nhu/Desktop/ProjectX/pyvideotrans/videotrans/translator/_transapi.py", line 43, in _item_task

if jsdata['code'] != 0:

KeyError: 'code'


=
system:macOS-26.4.1-arm64-arm-64bit
version:v3.99
frozen:False
language:en
root_dir:/Users/nhu/Desktop/ProjectX/pyvideotrans
Python: 3.10.19 (main, Feb 12 2026, 00:36:33) [Clang 21.1.4 ]

1 Replies

The translation failed because your custom translation API endpoint did not return the expected JSON field code. In TransAPI mode, pyVideoTrans requires the response to contain "code": 0 to indicate success; otherwise it raises a KeyError.

How to fix

  1. Check the actual response of your translation API (use curl or a tool to call it directly).
  2. Ensure the JSON reply has at least these fields:

    • "code": 0 (integer) — 0 means success
    • "text" or "data" — the translated text
  3. If your API uses a different structure, you must either:

    • adapt your API to match pyVideoTrans’s expected format, or
    • switch to a built‑in translation channel (e.g., DeepSeek, OpenAI, Google) that already conforms.

Example of a valid response

{
  "code": 0,
  "text": "The translated sentence here."
}

Please refer to the official guide for custom translation APIs:

Post Your Reply
Open source and free maintenance is not easy. If this project is helpful to you, please consider making a small donation to help the project continue to maintain and update.

Related resource