Back Home

GitHub Repo

Transformers Proposes Chat Template Compatibility Fix for 5.23.0 Message Object Crash Reports

A November 15 report found that Transformers 5.23.0 may throw an exception before applying a template when handling dataclass and Pydantic chat messages. A fix proposed the same day covers tokenizers and processors, but has not been merged; test results are currently from the submitter.

Marcus Qwertyus · Public domain · Image source
zh-Hant

On November 15, the Transformers community reported a regression in chat templates: dataclass and Pydantic message objects that worked in 5.22.2 trigger an AttributeError when apply_chat_template is called in 5.23.0 and in the main commit tested by the reporter. The reproduction includes OpenAI SDK’s ChatCompletionMessage. As a result, tool-calling workflows that put SDK responses directly back into the conversation history may fail when preparing input for the next turn. Issue report

Chat templates combine roles, content, and model-specific control tokens into an input sequence, making them an important part of the path from conversational data to generation. The official documentation demonstrates this with a list of dictionaries containing role and content. This suggests that integrations mixing message objects should be the first place to check; the report does not establish that all dictionary inputs are affected. Official documentation

The report traces the problem to the order of data processing: the code calls msg.items() to remove fields whose values are None before converting the object to a dictionary. Existing tests call a later-stage template-rendering function directly, so they did not cover the failure at the public entry point. This illustrates how testing only the lower-level rendering result can miss type compatibility problems at the entry point. Reproduction and analysis

PR #50775, proposed the same day, recommends using shared message-normalization logic to convert Mapping, dataclass, and Pydantic objects before removing empty fields. It also addresses a processor bug that misclassifies a single group of object messages as a batch of conversations. The submitter reports that 45 tests and 11 subtests pass, but the page still shows CI awaiting maintainer approval, so this does not yet count as upstream validation. Proposed fix

For engineering teams preparing to upgrade, a useful check is to run the SDK response objects they actually use through the complete template entry point, including cases with None fields and the processor path. Track the fix’s review, CI results, and inclusion in a release. Current evidence supports a compatibility regression for specific input types; it is not sufficient to conclude that model generation quality is affected.

Sources

  1. apply_chat_template crashes on dataclass / pydantic message objects
  2. Fix apply_chat_template crashing on dataclass and Pydantic message objects
  3. Transformers 官方文件:Chat templates