#1883·skills

extract_form_field_info.py silently drops every fillable text/choice field whose widgets are /Kids

Author: sxh313Created Sep 27, 2026Updated Sep 27, 2026

extract_form_field_info.py silently drops every fillable text/choice field whose widgets are /Kids

Summary

skills/pdf/scripts/extract_form_field_info.py omits a field from its JSON output whenever the field stores its widget annotation(s) in /Kids and its /FT is not /Btn. The field is real: PdfReader.get_fields() lists it, the skill's own check_fillable_fields.py reports "This PDF has fillable form fields", and pypdf writes a value into it fine — but the two documented steps refuse to see it:

extract_form_field_info.py  ->  Wrote 0 fields          (no warning on stderr/stdout)
fill_fillable_fields.py     ->  ERROR: `addr` is not a valid field ID
                                ERROR: `country` is not a valid field ID   (exit 1, no output PDF)

I built one form two legal ways — (a) the field dict is the widget annotation, (b) the field dict holds /T /FT /Kids and the widget holds /Rect /AP /Parent — with identical field names, rects and /Opt, and the same values.json for both. Only (b) fails.

Where

skills/pdf/scripts/extract_form_field_info.py:53-58 at main = 3337550:

python
    for field_id, field in fields.items():
        if field.get("/Kids"):
            if field.get("/FT") == "/Btn":
                possible_radio_names.add(field_id)
            continue                                    # also skips /Tx and /Ch
        field_info_by_id[field_id] = make_field_dict(field, field_id)

The continue is attached to the /Kids branch rather than to the /Btn sub-branch, so a non-button field with widget children never reaches field_info_by_id.

Why this reads as an oversight rather than a design choice

Three things in this same function only make sense if parented fields are meant to work:

  1. get_full_annotation_field_id() (lines 12-19) exists specifically to resolve a widget annotation to its parent field's qualified name by walking /Parent.
  2. The annotation loop then has if field_id in field_info_by_id: -> set ["page"], ["rect"] (lines 65-68). For a parented field that branch can never fire, because line 57 already removed it from field_info_by_id. The wiring that would attach a widget's rect to its parent field is present and dead.
  3. possible_radio_names is the /Kids case — handled, and rescued, for exactly one of the four field types the docs name.

/FT is an inherited field attribute (ISO 32000-1, Table 221), so "a field node carrying /T /FT /Kids plus separate widget annotations under /Parent" is the standard structure for multiple widgets per field, for inherited /DA//DV, and for Acrobat partial field names such as shipping -> shipping.name.

Reproduction

repro.py — run from the repo root (needs pypdf); it writes only to a temp dir:

python
"""Minimal repro: one form built two legal ways (terminal widget vs parent + /Kid widget).

Run from the repo root:  python repro.py      (or: python repro.py /path/to/skills/pdf/scripts)
Outputs go to a temp dir; nothing is written next to the scripts.
"""
import json
import subprocess
import sys
import tempfile
from pathlib import Path

from pypdf import PdfReader, PdfWriter
from pypdf.generic import (
    ArrayObject, DecodedStreamObject, DictionaryObject,
    NameObject, NumberObject, TextStringObject,
)

# path to the skill's scripts/ directory
SCRIPTS = Path(sys.argv[1]) if len(sys.argv) > 1 else Path("skills/pdf/scripts")
OUT = Path(tempfile.mkdtemp(prefix="pdf_repro_"))

FIELDS = [  # one text field + one dropdown, same names / rects / /Opt in both PDFs
    ("addr", "/Tx", [92, 580, 260, 592], {}),
    ("country", "/Ch", [92, 620, 260, 632],
     {"/Opt": ArrayObject([ArrayObject([TextStringObject("/1"), TextStringObject("USA")]),
                           ArrayObject([TextStringObject("/2"), TextStringObject("Canada")])])}),
]


def build(path, parented):
    """parented=False -> each field IS its widget (one dict). parented=True -> the field
    dict holds /T /FT /Kids and the widget annotation holds /Rect /AP /Parent."""
    w = PdfWriter()
    page = w.add_blank_page(612, 792)
    font = w._add_object(DictionaryObject({
        NameObject("/Type"): NameObject("/Font"),
        NameObject("/Subtype"): NameObject("/Type1"),
        NameObject("/BaseFont"): NameObject("/Helvetica")}))
    res = w._add_object(DictionaryObject({
        NameObject("/Font"): DictionaryObject({NameObject("/Helv"): font})}))
    annots, fields = [], []
    for t, ft, rect, extra in FIELDS:
        s = DecodedStreamObject()
        s[NameObject("/Type")] = NameObject("/XObject")
        s[NameObject("/Subtype")] = NameObject("/Form")
        s[NameObject("/BBox")] = ArrayObject([NumberObject(0), NumberObject(0),
                                              NumberObject(int(rect[2] - rect[0])),
                                              NumberObject(int(rect[3] - rect[1]))])
        s[NameObject("/Resources")] = res
        s.set_data(b"BT /Helv 10 Tf 0 g (x) Tj ET")
        stream = w._add_object(s)
        widget = DictionaryObject({
            NameObject("/Type"): NameObject("/Annot"),
            NameObject("/Subtype"): NameObject("/Widget"),
            NameObject("/Rect"): ArrayObject([NumberObject(x) for x in rect]),
            NameObject("/AP"): DictionaryObject({NameObject("/N"): stream})})
        if not parented:
            widget[NameObject("/T")] = TextStringObject(t)
            widget[NameObject("/FT")] = NameObject(ft)
            for k, v in extra.items():
                widget[NameObject(k)] = v
            ref = w._add_object(widget)
            fields.append(ref)
        else:
            ref = w._add_object(widget)
            fld = DictionaryObject({
                NameObject("/T"): TextStringObject(t),
                NameObject("/FT"): NameObject(ft),
                NameObject("/Kids"): ArrayObject([ref])})
            for k, v in extra.items():
                fld[NameObject(k)] = v
            fld_ref = w._add_object(fld)
            widget[NameObject("/Parent")] = fld_ref
            fields.append(fld_ref)
        annots.append(ref)
    form = DictionaryObject({
        NameObject("/Fields"): ArrayObject(fields),
        NameObject("/DA"): TextStringObject("/Helv 10 Tf 0 g"),
        NameObject("/DR"): res})
    w._root_object[NameObject("/AcroForm")] = w._add_object(form)
    page[NameObject("/Annots")] = ArrayObject(annots)
    with open(path, "wb") as f:
        w.write(f)


def run(script, *args):
    p = subprocess.run([sys.executable, str(SCRIPTS / script), *map(str, args)],
                       capture_output=True, text=True)
    return p.returncode, (p.stdout + p.stderr).strip()


values = OUT / "values.json"
values.write_text(json.dumps([
    {"field_id": "addr", "description": "address", "page": 1, "value": "123 Main St"},
    {"field_id": "country", "description": "country", "page": 1, "value": "/1"},
]), encoding="utf-8")

print("work dir:", OUT)
for tag, parented in (("terminal_widgets", False), ("parent_with_widget_kid", True)):
    pdf = OUT / f"{tag}.pdf"
    build(pdf, parented)
    print("=" * 70)
    print(tag)
    print("  pypdf get_fields() keys:", sorted((PdfReader(str(pdf)).get_fields() or {})))
    rc, out = run("check_fillable_fields.py", pdf)
    print(f"  check_fillable_fields.py rc={rc}: {out}")
    j = OUT / f"{tag}.json"
    rc, out = run("extract_form_field_info.py", pdf, j)
    print(f"  extract_form_field_info.py rc={rc}: {out}")
    print("    ->", json.dumps(json.loads(j.read_text(encoding='utf-8'))))
    rc, out = run("fill_fillable_fields.py", pdf, values, OUT / f"{tag}_filled.pdf")
    print(f"  fill_fillable_fields.py rc={rc}: {out}")
    if rc == 0:
        print("    /V after fill:", {k: v.get("/V") for k, v in
                                    PdfReader(str(OUT / f"{tag}_filled.pdf")).get_fields().items()})

Observed on main @ 3337550, Python 3.14.6, pypdf 6.19.0, Windows:

work dir: C:\Users\28697\AppData\Local\Temp\pdf_repro_ev8nplw9
======================================================================
terminal_widgets
  pypdf get_fields() keys: ['addr', 'country']
  check_fillable_fields.py rc=0: This PDF has fillable form fields
  extract_form_field_info.py rc=0: Wrote 2 fields to C:\Users\28697\AppData\Local\Temp\pdf_repro_ev8nplw9\terminal_widgets.json
    -> [{"field_id": "country", "type": "choice", "choice_options": [{"value": "/1", "text": "USA"}, {"value": "/2", "text": "Canada"}], "page": 1, "rect": [92, 620, 260, 632]}, {"field_id": "addr", "type": "text", "page": 1, "rect": [92, 580, 260, 592]}]
  fill_fillable_fields.py rc=0: 
    /V after fill: {'addr': '123 Main St', 'country': '/1'}
======================================================================
parent_with_widget_kid
  pypdf get_fields() keys: ['addr', 'country']
  check_fillable_fields.py rc=0: This PDF has fillable form fields
  extract_form_field_info.py rc=0: Wrote 0 fields to C:\Users\28697\AppData\Local\Temp\pdf_repro_ev8nplw9\parent_with_widget_kid.json
    -> []
  fill_fillable_fields.py rc=1: ERROR: `addr` is not a valid field ID
ERROR: `country` is not a valid field ID

Proposed fix

Move the continue into the /Btn branch, and keep skipping nodes that only carry a name (a grouping node declares no /FT, so it is not a field):

diff
--- a/skills/pdf/scripts/extract_form_field_info.py
+++ b/skills/pdf/scripts/extract_form_field_info.py
@@ -54,7 +54,10 @@
         if field.get("/Kids"):
             if field.get("/FT") == "/Btn":
                 possible_radio_names.add(field_id)
-            continue
+                continue
+            # A node with no /FT is a name grouping, not a field.
+            if not field.get("/FT"):
+                continue
         field_info_by_id[field_id] = make_field_dict(field, field_id)
 
 

Same repro.py, with only that change applied:

work dir: C:\Users\28697\AppData\Local\Temp\pdf_repro_2_1yk2h8
======================================================================
terminal_widgets
  pypdf get_fields() keys: ['addr', 'country']
  check_fillable_fields.py rc=0: This PDF has fillable form fields
  extract_form_field_info.py rc=0: Wrote 2 fields to C:\Users\28697\AppData\Local\Temp\pdf_repro_2_1yk2h8\terminal_widgets.json
    -> [{"field_id": "country", "type": "choice", "choice_options": [{"value": "/1", "text": "USA"}, {"value": "/2", "text": "Canada"}], "page": 1, "rect": [92, 620, 260, 632]}, {"field_id": "addr", "type": "text", "page": 1, "rect": [92, 580, 260, 592]}]
  fill_fillable_fields.py rc=0: 
    /V after fill: {'addr': '123 Main St', 'country': '/1'}
======================================================================
parent_with_widget_kid
  pypdf get_fields() keys: ['addr', 'country']
  check_fillable_fields.py rc=0: This PDF has fillable form fields
  extract_form_field_info.py rc=0: Wrote 2 fields to C:\Users\28697\AppData\Local\Temp\pdf_repro_2_1yk2h8\parent_with_widget_kid.json
    -> [{"field_id": "country", "type": "choice", "choice_options": [{"value": "/1", "text": "USA"}, {"value": "/2", "text": "Canada"}], "page": 1, "rect": [92, 620, 260, 632]}, {"field_id": "addr", "type": "text", "page": 1, "rect": [92, 580, 260, 592]}]
  fill_fillable_fields.py rc=0: 
    /V after fill: {'addr': '123 Main St', 'country': '/1'}

After the fix the two structures produce a byte-identical field_info.json, and both fill to rc=0 with the values present in the written PDF (/V = 123 Main St / /1).

No-regression check

I ran 5 fixtures through the pristine and the patched scripts and diffed the JSON per field id. Nothing was removed and nothing that existed before changed:

fixture structure extracted before extracted after fill rc before fill rc after
A terminal /Tx + /Btn last_name, agree same, identical dicts 0 0
B parented /Tx + /Btn agree only + last_name 1 0
C parented /Ch + /Tx with 2 widgets (none) + country, addr 1 0
D terminal /Ch + /Tx country, addr same, identical dicts 0 0
E shipping (no /FT) -> shipping.name (/Tx) -> widget (none) + shipping.name only 1 0

Totals: 4 fields added, 0 removed, 0 existing entries modified. Fixture E is the guard case: the name-only grouping node shipping is still not emitted, no new "Unable to determine location for field id" warning appears, and the value lands on the leaf (/V: shipping = None, shipping.name = "Homer"). The /Btn path is untouched — a parented checkbox is still reported as radio_group, which is existing behaviour and not something this change attempts to alter.

What I could not measure

I scanned the 2,297 PDFs on this machine and none carries an AcroForm, so I cannot quote a real-world frequency for parented text/choice fields. The case here is structural: the spec allows the shape, and this function already contains the (currently unreachable) code that was written to handle it.