Skip to content

[BUG] #4927 (fix of #4858) incomplete: tools from HF datasets still don't render as at inference (JSON-string tools column dropped; Arrow-unified tool schemas gain null properties) #4934

Closed

Description

@Qubitium

Found on main @ 3867c82, CPU (tokenizer-only path), transformers 5.22.1, datasets as installed.

What is wrong

#4927 (fix of #4858) forwards a row's tools/documents to apply_chat_template exactly as stored (gptqmodel/utils/calibration.py:776, template_inputs[key] = value). It does not handle the two layouts that function-calling calibration data on the HF Hub actually arrives in:

  1. tools stored as a JSON string. Many HF tool-calling datasets store tools as a JSON string column, because Arrow cannot hold heterogeneous schemas. apply_chat_template raises ValueError: Tools should either be a JSON schema, or a callable ..., and fix(calibration): render a messages row with its tools and documents (#4858) #4927's fallback re-renders the row without the tools. The schema still never reaches the Hessian, the exact [BUG] Calibration messages rows: the tools column is silently dropped; tool schemas never reach the Hessian (101 of 236 intended tokens on Qwen2.5) #4858 symptom, with one warning for the whole run.
  2. tools stored as a list of dicts in an HF Dataset. Arrow unifies the struct schema across rows, so every row's schema gains every other row's parameter names as null, for example "properties": {"city": {...}, "tz": null}. The rendered tool block then differs from what inference sends. Nothing is logged.

Repro (Qwen2.5-0.5B-Instruct tokenizer, two rows with different one-tool schemas)

msgs = [{"role":"user","content":"What's the weather in Paris?"},{"role":"assistant","content":"Sunny."}]
# t1 = get_weather(city), t2 = get_time(tz)
ref = lambda t: len(tok(tok.apply_chat_template(msgs, tools=t, tokenize=False), add_special_tokens=False)["input_ids"])
prepare_calibration_dataset(m, calibration_dataset=[{"messages":msgs,"tools":json.dumps(t1)}, {"messages":msgs,"tools":json.dumps(t2)}], batch_size=1)
prepare_calibration_dataset(m, calibration_dataset=Dataset.from_list([{"messages":msgs,"tools":t1}, {"messages":msgs,"tools":t2}]), batch_size=1)
prepare_calibration_dataset(m, calibration_dataset=Dataset.from_list([{"messages":msgs,"tools":json.dumps(t1)}, {"messages":msgs,"tools":json.dumps(t2)}]), batch_size=1)
ref no tools 41   ref t1 182   ref t2 179
WARN  Quantize: this tokenizer's chat template rejected the calibration row's ['tools']; rendering without them: ValueError: Tools should either be a JSON schema, ...
list rows, JSON-string tools   [41, 41]     <- tools dropped (expected 182, 179)
HF Dataset, list tools         [186, 184]   <- schema rendered with injected nulls:
   {"type": "function", "function": {"name": "get_weather", ..., "properties": {"city": {...}, "tz": null}, ...}}
HF Dataset, JSON-string tools  [41, 41]     <- tools dropped

A Python list of dict rows with real dict tools works (208/208 on a one-tool row; Llama-3.2-1B 229/229, Qwen3-0.6B 196/196).

Suggested fix

Normalize the template inputs before forwarding them. json.loads a str value of tools/documents, and recursively drop None values that Arrow injected into dict schemas (a JSON schema never legitimately carries a null property definition). The rejecting-template fallback should stay only for templates that truly don't support tools.

Test that should have caught it

tests/test_calibration_messages_tools_4858.py only feeds Python lists of dict rows. It needs one case with a datasets.Dataset built from rows with different tool schemas, and one with a JSON-string tools column, each asserting token equality with apply_chat_template(messages, tools=<original list>).

Severity: low-medium. Calibration on HF function-calling datasets still misses or distorts the tool schema block. That is the #4858 accuracy gap, now with a warning in case 1 and silent in case 2.

🤖 Generated with Claude Code

Activity

  1. Qubitium commented on Nov 17, 2026

    @Qubitium
    CollaboratorAuthor

    Agent claim: agent-root-4934 is working on this issue now.

  2. Qubitium commented on Nov 17, 2026

    @Qubitium
    CollaboratorAuthor

    Agent claim released: agent-53ba17eb4d83 finished this issue.

    Pull request: #5004

    HF-datasets tools must reach the chat template exactly as at inference

  3. Qubitium commented on Nov 17, 2026

    @Qubitium
    CollaboratorAuthor

    Agent claim released: agent-root-4934 finished this issue.

    Pull request: #4997 — squash-merged to main as 9c5efff3494ec416442225a0aae79eda398a1013.

    What landed (gptqmodel/utils/calibration.py + two regression files):

    • _normalize_template_input_value() decodes an object/array JSON string and drops the None mapping entries Arrow injects into a unified struct schema, while keeping a real JSON-schema null ({"type": "null"}) and None list elements; containers are copied, so the caller's rows are not mutated. A plain, non-JSON string ("the weather in Paris", "42", "true") is forwarded unchanged.
    • _normalize_template_input_payload() additionally presents a one-tool tools payload as the list the chat templates iterate (a one-object column was rejected outright).
    • _tokenize_messages_value() now runs both before calling the template, so the schema block the model sees at inference is the one that reaches the Hessian. The rejecting-template fallback and its one-shot warning are unchanged.
    • tests/test_calibration_messages_tools_4858.py (+5 nodes, +1 real-tokenizer matrix node) and tests/test_calibration_tools_formats_4934.py (ported from fix(calibration): normalize the tool schemas HF datasets store (#4934) #5004, all 5 nodes kept unweakened). Both run in the pre-merge AWQ CPU (unit) suite; gptqmodel/utils/calibration.py and tests/test_calibration_*.py now trigger that workflow.
    • Three calibration.py citations in docs/decision-transformer-calibration.md are symbol-anchored and guarded by tests/test_docs_contract_refs.py.

    Red → green: with both regression files copied onto unmodified origin/main (2be068cc6): 9 failed, 7 passed; on the merged tree: 39 passed together with the docs contract. Real-tokenizer evidence: reference 164/164 ids; before, every Hub layout rendered 41/41 (tools dropped); after, list rows, Dataset rows, Dataset[:] and with_format("numpy"|"pandas"|"arrow")[:] all render 164/164.

    PR #5004 (fix/issue-4934, the duplicate that added tests/test_calibration_tools_formats_4934.py) is closed as superseded, with a comment listing the adjustments.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions