PaddleOCR icon indicating copy to clipboard operation
PaddleOCR copied to clipboard

运行huggingface上https://huggingface.co/PaddlePaddle/PaddleOCR-VL 的demo 但是chat template会报错 TypeError: can only concatenate str (not "list") to str

Open hwang136 opened this issue 1 month ago • 7 comments

🔎 Search before asking

  • [x] I have searched the PaddleOCR Docs and found no similar bug report.
  • [x] I have searched the PaddleOCR Issues and found no similar bug report.
  • [x] I have searched the PaddleOCR Discussions and found no similar bug report.

🐛 Bug (问题描述)

运行huggingface上https://huggingface.co/PaddlePaddle/PaddleOCR-VL 的demo 但是chat template会报错

TypeError Traceback (most recent call last) Cell In[20], line 35 25 processor = AutoProcessor.from_pretrained(model_path, trust_remote_code=True) 27 messages = [ 28 {"role": "user",
29 "content": [ (...) 33 } 34 ] ---> 35 inputs = processor.apply_chat_template( 36 messages, 37 tokenize=True, 38 add_generation_prompt=True, 39 return_dict=True, 40 return_tensors="pt" 41 ).to(DEVICE) 43 outputs = model.generate(**inputs, max_new_tokens=1024) 44 outputs = processor.batch_decode(outputs, skip_special_tokens=True)[0]

File ~/.local/lib/python3.10/site-packages/transformers/utils/deprecation.py:172, in deprecate_kwarg..wrapper..wrapped_func(*args, **kwargs) 168 elif minimum_action in (Action.NOTIFY, Action.NOTIFY_ALWAYS) and not is_torchdynamo_compiling(): 169 # DeprecationWarning is ignored by default, so we use FutureWarning instead 170 warnings.warn(message, FutureWarning, stacklevel=2) --> 172 return func(*args, **kwargs)

File ~/.local/lib/python3.10/site-packages/transformers/utils/deprecation.py:172, in deprecate_kwarg..wrapper..wrapped_func(*args, **kwargs) 168 elif minimum_action in (Action.NOTIFY, Action.NOTIFY_ALWAYS) and not is_torchdynamo_compiling(): 169 # DeprecationWarning is ignored by default, so we use FutureWarning instead 170 warnings.warn(message, FutureWarning, stacklevel=2) --> 172 return func(*args, **kwargs)

File ~/.local/lib/python3.10/site-packages/transformers/processing_utils.py:1675, in ProcessorMixin.apply_chat_template(self, conversation, chat_template, **kwargs) 1672 batch_images.append(images) 1673 batch_videos.append(videos) -> 1675 prompt, generation_indices = render_jinja_template( 1676 conversations=conversations, 1677 chat_template=chat_template, 1678 **processed_kwargs["template_kwargs"], # different flags such as return_assistant_mask 1679 **self.tokenizer.special_tokens_map, # tokenizer special tokens are used by some templates 1680 ) 1682 if not is_batched: 1683 prompt = prompt[0]

File ~/.local/lib/python3.10/site-packages/transformers/utils/chat_template_utils.py:539, in render_jinja_template(conversations, tools, documents, chat_template, return_assistant_tokens_mask, continue_final_message, add_generation_prompt, **kwargs) 537 all_generation_indices.append(generation_indices) 538 else: --> 539 rendered_chat = compiled_template.render( 540 messages=chat, 541 tools=tool_schemas, 542 documents=documents, 543 add_generation_prompt=add_generation_prompt, 544 **kwargs, 545 ) 546 if continue_final_message: 547 if (final_message.strip() not in rendered_chat) or ( 548 continue_final_message_tag.strip() not in rendered_chat 549 ):

File /usr/local/lib/python3.10/dist-packages/jinja2/environment.py:1295, in Template.render(self, *args, **kwargs) 1293 return self.environment.concat(self.root_render_func(ctx)) # type: ignore 1294 except Exception: -> 1295 self.environment.handle_exception()

File /usr/local/lib/python3.10/dist-packages/jinja2/environment.py:942, in Environment.handle_exception(self, source) 937 """Exception handling helper. This is used internally to either raise 938 rewritten exceptions or return a rendered traceback for the template. 939 """ 940 from .debug import rewrite_traceback_stack --> 942 raise rewrite_traceback_stack(source=source)

File

TypeError: can only concatenate str (not "list") to str

🏃‍♂️ Environment (运行环境)

Package Version


aiohappyeyeballs 2.6.1 aiohttp 3.12.15 aiosignal 1.4.0 alembic 1.13.3 annotated-types 0.7.0 anyio 3.7.1 argon2-cffi 25.1.0 argon2-cffi-bindings 21.2.0 arrow 1.3.0 asgiref 3.10.0 astor 0.8.1 asttokens 3.0.0 async-generator 1.10 async-lru 2.0.5 async-timeout 5.0.1 attrs 25.3.0 babel 2.17.0 backcall 0.2.0 beautifulsoup4 4.14.0 blake3 1.0.7 bleach 6.2.0 cachetools 6.2.0 cbor2 5.7.0 certifi 2025.10.5 cffi 2.0.0 charset-normalizer 3.4.3 click 8.3.0 cloudpickle 3.1.1 comm 0.2.3 compressed-tensors 0.11.0 cupy-cuda12x 13.6.0 debugpy 1.8.16 decorator 5.2.1 defusedxml 0.7.1 depyf 0.19.0 dill 0.4.0 diskcache 5.6.3 distro 1.9.0 Django 5.2.6 dnspython 2.8.0 einops 0.8.1 email-validator 2.3.0 entrypoints 0.4 exceptiongroup 1.3.0 executing 2.2.1 fastapi 0.118.0 fastapi-cli 0.0.13 fastapi-cloud-cli 0.3.0 fastjsonschema 2.21.2 fastrlock 0.8.3 filelock 3.19.1 flashinfer-python 0.3.0 fqdn 1.5.1 frozendict 2.4.6 frozenlist 1.7.0 fsspec 2024.10.0 gguf 0.17.1 greenlet 3.2.4 h11 0.16.0 hf-xet 1.1.7 httpcore 1.0.9 httptools 0.6.4 httpx 0.28.1 huggingface-hub 0.35.3 idna 3.10 image 1.5.33 interegular 0.3.3 ipykernel 6.17.0 ipython 8.12.0 ipython-genutils 0.2.0 ipywidgets 8.1.7 isoduration 20.11.0 jedi 0.19.2 Jinja2 3.1.6 jiter 0.11.0 joblib 1.5.2 json5 0.12.0 jsonpointer 3.0.0 jsonschema 4.24.0 jsonschema-specifications 2025.9.1 jupyter 1.1.1 jupyter_client 7.4.4 jupyter-console 6.4.4 jupyter-contrib-core 0.4.2 jupyter-contrib-nbextensions 0.7.0 jupyter_core 4.11.2 jupyter-events 0.12.0 jupyter-highlight-selected-word 0.2.0 jupyter-lsp 2.2.5 jupyter_nbextensions_configurator 0.6.4 jupyter-server 1.21.0 jupyter_server_terminals 0.5.3 jupyterhub 1.0.0.dev0 jupyterlab 3.0.16 jupyterlab-pygments 0.2.2 jupyterlab_server 2.27.3 jupyterlab_widgets 3.0.15 lark 1.2.2 llguidance 0.7.30 llvmlite 0.44.0 lm-format-enforcer 0.11.3 lxml 6.0.0 Mako 1.3.10 markdown-it-py 4.0.0 MarkupSafe 3.0.3 matplotlib-inline 0.1.7 mdurl 0.1.2 mistral_common 1.8.5 mistune 2.0.5 mpmath 1.3.0 msgpack 1.1.1 msgspec 0.19.0 multidict 6.6.4 nbclassic 0.5.6 nbclient 0.7.0 nbconvert 7.8.0 nbformat 5.10.3 nest-asyncio 1.6.0 networkx 3.4.2 ninja 1.13.0 notebook 6.4.0 notebook_shim 0.2.4 numba 0.61.2 numpy 1.26.4 nvidia-cublas-cu12 12.8.4.1 nvidia-cuda-cupti-cu12 12.8.90 nvidia-cuda-nvrtc-cu12 12.8.93 nvidia-cuda-runtime-cu12 12.8.90 nvidia-cudnn-cu12 9.10.2.21 nvidia-cudnn-frontend 1.14.1 nvidia-cufft-cu12 11.3.3.83 nvidia-cufile-cu12 1.13.1.3 nvidia-curand-cu12 10.3.9.90 nvidia-cusolver-cu12 11.7.3.90 nvidia-cusparse-cu12 12.5.8.93 nvidia-cusparselt-cu12 0.7.1 nvidia-ml-py 13.580.82 nvidia-nccl-cu12 2.27.3 nvidia-nvjitlink-cu12 12.8.93 nvidia-nvtx-cu12 12.8.90 oauthlib 3.3.1 openai 2.1.0 openai-harmony 0.0.4 opencv-python-headless 4.11.0.86 outlines_core 0.2.11 overrides 7.7.0 packaging 25.0 pamela 1.2.0 pandas 2.2.3 pandocfilters 1.5.1 parso 0.8.5 partial-json-parser 0.2.1.1.post4 pexpect 4.9.0 pickleshare 0.7.5 pillow 11.3.0 pip 25.0.1 platformdirs 4.4.0 prometheus_client 0.23.1 prometheus-fastapi-instrumentator 7.1.0 prompt_toolkit 3.0.52 propcache 0.4.0 protobuf 6.32.1 psutil 7.0.0 ptyprocess 0.7.0 pure_eval 0.2.3 py-cpuinfo 9.0.0 pyarrow 18.0.0 pyavi 0.0.29 pybase64 1.4.2 pycountry 24.6.1 pycparser 2.22 pycryptodomex 3.21.0 pydantic 2.12.0 pydantic_core 2.41.1 pydantic-extra-types 2.10.5 Pygments 2.16.1 pynvml 13.0.1 pyorc 0.9.0 python-dateutil 2.9.0.post0 python-dotenv 1.1.1 python-json-logger 3.3.0 python-multipart 0.0.20 pytz 2025.2 PyYAML 6.0.3 pyzmq 26.4.0 ray 2.49.2 referencing 0.36.2 regex 2025.9.18 requests 2.32.5 rfc3339-validator 0.1.4 rfc3986-validator 0.1.1 rich 14.1.0 rich-toolkit 0.15.1 rignore 0.7.0 rpds-py 0.20.1 safetensors 0.6.2 scikit-learn 1.5.2 scipy 1.15.3 Send2Trash 1.8.3 sentencepiece 0.2.1 sentry-sdk 2.39.0 setproctitle 1.3.7 setuptools 59.6.0 shellingham 1.5.4 six 1.17.0 sniffio 1.3.1 soundfile 0.13.1 soupsieve 2.6 soxr 0.5.0.post1 SQLAlchemy 2.0.43 sqlparse 0.5.3 stack-data 0.6.3 starlette 0.48.0 sympy 1.14.0 terminado 0.17.0 threadpoolctl 3.6.0 tiktoken 0.11.0 tinycss2 1.4.0 tokenizers 0.22.0 tomli 2.2.1 torch 2.8.0 torchaudio 2.8.0 torchvision 0.23.0 tornado 6.2 tqdm 4.67.1 traitlets 5.5.0 transformers 4.57.1 triton 3.4.0 typer 0.19.2 types-python-dateutil 2.9.0.20250708 typing_extensions 4.15.0 typing-inspection 0.4.2 tzdata 2025.2 uri-template 1.3.0 urllib3 2.5.0 uvicorn 0.37.0 uvloop 0.21.0 vllm 0.11.0 watchfiles 1.1.0 wcwidth 0.2.13 webcolors 24.11.1 webencodings 0.5.1 websocket-client 1.8.0 websockets 15.0.1 wheel 0.37.1 widgetsnbextension 4.0.14 xformers 0.0.32.post1 xgrammar 0.1.25 yarl 1.21.0

🌰 Minimal Reproducible Example (最小可复现问题的Demo)

`from PIL import Image import torch from transformers import AutoModelForCausalLM, AutoProcessor

---- Settings ----

model_path = "PaddlePaddle/PaddleOCR-VL" image_path = "test.png" task = "ocr" # Options: 'ocr' | 'table' | 'chart' | 'formula'

------------------

DEVICE = "cuda" if torch.cuda.is_available() else "cpu"

PROMPTS = { "ocr": "OCR:", "table": "Table Recognition:", "formula": "Formula Recognition:", "chart": "Chart Recognition:", }

image = Image.open(image_path).convert("RGB")

model = AutoModelForCausalLM.from_pretrained( model_path, trust_remote_code=True, torch_dtype=torch.bfloat16 ).to(DEVICE).eval() processor = AutoProcessor.from_pretrained(model_path, trust_remote_code=True)

messages = [ {"role": "user",
"content": [ {"type": "image", "image": image}, {"type": "text", "text": PROMPTS[task]}, ] } ] inputs = processor.apply_chat_template( messages, tokenize=True, add_generation_prompt=True, return_dict=True, return_tensors="pt" ).to(DEVICE)

outputs = model.generate(**inputs, max_new_tokens=1024) outputs = processor.batch_decode(outputs, skip_special_tokens=True)[0] print(outputs)`

hwang136 avatar Nov 20 '25 03:11 hwang136

@TingquanGao 大佬 这个问题能复现出来么 需要我提供什么额外的信息么

hwang136 avatar Nov 21 '25 03:11 hwang136

@TingquanGao 大佬抽空看一下?

hwang136 avatar Nov 24 '25 07:11 hwang136

same issue

windygoo avatar Nov 24 '25 15:11 windygoo

@hwang136 是不是很久没更新权重了?发一下权重里的chat_template看看

zhang-prog avatar Nov 26 '25 09:11 zhang-prog

{%- if not add_generation_prompt is defined -%} {%- set add_generation_prompt = true -%} {%- endif -%} {%- if not cls_token is defined -%} {%- set cls_token = "<|begin_of_sentence|>" -%} {%- endif -%} {%- if not sep_token is defined -%} {%- set sep_token = "<|end_of_sentence|>" -%} {%- endif -%} {{- cls_token -}} {%- for message in messages -%} {%- if message["role"] == "user" -%} {{- "User: <|IMAGE_START|><|IMAGE_PLACEHOLDER|><|IMAGE_END|>" + message["content"] + "\n" -}} {%- elif message["role"] == "assistant" -%} {{- "Assistant: " + message["content"] + sep_token -}} {%- elif message["role"] == "system" -%} {{- message["content"] -}} {%- endif -%} {%- endfor -%} {%- if add_generation_prompt -%} {{- "Assistant: " -}} {%- endif -%}

hwang136 avatar Nov 28 '25 09:11 hwang136

@zhang-prog 大佬查收

hwang136 avatar Nov 28 '25 13:11 hwang136

看着是chat_template太老了,可以更新下最新的:https://huggingface.co/PaddlePaddle/PaddleOCR-VL/blob/main/chat_template.jinja

zhang-prog avatar Dec 04 '25 06:12 zhang-prog