🌉 bridged from @searchapipractices@programming.dev by Tidepool
searchapipractices
@searchapipractices.programming.dev.ap.tdpl.io
The Images response returns display_link, a field its own schema never documents
A small image search panel needed one line of text under every thumbnail: the site the image came from. The response already had it. When I generated a model from the field table on the endpoint page and parsed the documented example, that line went blank. The field carrying it was in the payload, not in my model, and the parser had been told to accept only what the model declares. The page in question is
/docs/google_images_search, and the field is calleddisplay_link.What the example returns
The documented example response is real and shortened, and its first image row starts like this:
"display_link": "en.wikipedia.org","display_url": "en.wikipedia.org","domain": "en.wikipedia.org"(checked 2026-11-30, /docs/google_images_search). The same row also carriesimage_url,link,position,rank,source,thumbnail,thumbnail_url,titleandurl. The second row repeats the pattern, withdisplay_linkset tokids.nationalgeographic.comanddisplay_urlanddomainholding the same string. In both rows the three domain-shaped fields agree, which is convenient for reading the example and tells you nothing about how often they agree in general. So the string I wanted was sitting in the payload, spelled as a first-class key, on the level of the item.What the schema lists
Directly below the example, the page has a table titled
ImageResultResult item schema, and it enumerates the row fields one by one:rank,position,title,link,url,source_url,display_url,image_url,thumbnail_url,thumbnail,source,domain(checked 2026-11-30, /docs/google_images_search). Twelve entries.display_linkis not one of them.display_urlis, described as "Display URL or source domain", anddomainis, described as "Source page domain". The table gives availability for each of these;rank,linkandimage_urlare marked required, and the rest are conditional in some way. The page also carries a note under its parameters: "Some Google Images payload shapes expose rank/link but not every normalized alias. Treat url, display_url, source_url, position, and thumbnail as optional." That note is about which aliases you should expect in the payload, and it namesdisplay_urlamong the aliases whose presence you should not count on. This is not a quirk of one page either. The News example row shows"display_link": "www.photonics.com", and itsNewsResulttable, read end to end, has no entry for the field either (checked 2026-11-30, /docs/google_news_search). The Videos example shows"display_link": "www.youtube.com", and itsVideoResulttable likewise stops atthumbnailwithout naming it (checked 2026-11-30, /docs/google_videos_search). Three examples, three tables, one key present in the data and absent from the documentation of the row. One more difference is worth writing down while the tables are open, because it decides which fields you can make non-optional. Images markstitleas optional, with the note "When image title or alt text is parsed." News markstitlerequired, Videos markstitlerequired, and Web Search marksOrganicResult.titlerequired (checked 2026-11-30, /docs/google_images_search, /docs/google_news_search, /docs/google_videos_search, /docs/google_web_search). I have not seen an Images response without a title; the difference I am reporting is the one printed in the tables, not an observation from traffic.Why a strict model drops it
If you build the model from the table,
display_linkis an extra key. What happens next depends entirely on one setting. In Pydantic,extra="forbid"raises a validation error naming the field, so the parse fails loudly and the whole batch is lost unless you catch it per item. The defaultextra="ignore"drops the key and hands you a valid object with the field simply missing, so the panel renders an empty line and nothing in the logs says why. TypeScript is the quiet version of the same story: a structural type with twelve members accepts an object with a thirteenth key, and that key is unreachable without a cast. There is a trap in the loud version too. If you run withforbidin staging and switch toignoreto keep production alive, you have not fixed the model, you have hidden the reason it was failing. The drop and the exception are two symptoms of the same gap between the table and the payload. I should be explicit about what the documentation does and does not say here. The sentence I can point at sits in the response section: a field that is not on the page for a request is omitted, with no empty stand-in returned in its place (checked 2026-11-30, /docs/google_web_search). That sentence is about the server omitting empty fields. It says nothing about what a client should do with a key that arrived but was never listed. Reading it as permission to ignore unknown keys is my inference, not a rule the page states.What to use instead
Two fields in the table answer the same question, and one of them is the one you can model.
display_urlis described as "Display URL or source domain", anddomainas "Source page domain". Both are conditional, and neither is guaranteed on every row. The practical shape is a small function with a preference order:display_urlfirst,domainsecond, empty string last, so the panel has a defined behaviour instead of an exception. The cost of that substitution is real and I would rather name it than hide it. In the documented example the two values are hostnames and they match, but the description ofdisplay_urlallows a display URL, which is not necessarily a bare host.domainis derived from the source page and may be absent exactly where the page has no derivable domain. Anddisplay_link, which I am not modelling, is described by no table entry at all, so I cannot tell you how its value would differ from the other two on a row where all three appear. I am choosing the fields with a written definition over the one without, and accepting that the visible string may change shape. For the model itself, the setting I now use isextra="allow"on these response models, with the unknown keys logged per endpoint and reviewed later. Allow does not invent data and it does not hide anything: the parse succeeds, the extras land in one place, and the log tells me which endpoint is sending keys the table does not carry. Forbidding unknown keys is the right default when you own both sides of the contract; here one side is a search page.The check
This is the script I keep next to the model. It parses with either policy, reports which keys the table did not describe, and resolves the source string with the fallback order above. It uses a dataclass and a hand-written policy check rather than importing Pydantic, so it runs on the standard library; the three policy names mirror the
extravalues above, andunknownstands in for Pydantic'smodel_extra.python
from dataclasses import dataclass, fields class UnknownFieldError(Exception): pass @dataclass class ImageResult: rank: int link: str image_url: str title: str = None position: int = None url: str = None source_url: str = None display_url: str = None thumbnail_url: str = None thumbnail: str = None source: str = None domain: str = None KNOWN = {f.name for f in fields(ImageResult)} def parse_row(row, policy="ignore"): if policy not in ("ignore", "forbid", "allow"): raise ValueError(policy) extra = sorted(k for k in row if k not in KNOWN) if extra and policy == "forbid": raise UnknownFieldError(", ".join(extra)) model = ImageResult(**{k: v for k, v in row.items() if k in KNOWN}) if policy == "allow": model.unknown = {k: row[k] for k in extra} return model, extra def source_label(row): for key in ("display_url", "domain"): value = row.get(key) if value: return value, key return "", "none" if __name__ == "__main__": documented = { "display_link": "en.wikipedia.org", "display_url": "en.wikipedia.org", "domain": "en.wikipedia.org", "rank": 1, "link": "https://en.wikipedia.org/wiki/Red_panda", "image_url": "https://upload.wikimedia.org/red-panda.jpg", "title": "Red panda - Wikipedia", } for policy in ("ignore", "forbid", "allow"): try: model, extra = parse_row(documented, policy) print(policy, "ok", "unsupported:", extra, "source:", source_label(documented)) except UnknownFieldError as err: print(policy, "raised for:", err) quiet = {"rank": 2, "link": "https://example.test/a", "image_url": "https://example.test/a.jpg"} print("no domain fields:", parse_row(quiet)[0].display_url, source_label(quiet)) domain_only = {"rank": 3, "link": "https://example.test/b", "image_url": "https://example.test/b.jpg", "domain": "example.test"} print("domain only:", source_label(domain_only))Three rows go through it, and the output is the argument for the policy I chose.
ignoreparses the documented row and reportsdisplay_linkas unsupported, with the label recovered fromdisplay_url.forbidraises on the same row, which is the failure that stopped my first pipeline.allowkeeps the key visible in one place for logging. The last two lines of output come from the other two rows, in that order: the row with neither domain field prints an empty label with the keynone, so a caller can render a fallback instead of a blank line that reads like a rendering fault, and the row that carries onlydomainresolves to that value whendisplay_urlis absent, which is the whole point of the fallback order. The general lesson I took is smaller than the field. When a model is generated from a table, the table is the contract I can see and the payload is the contract I actually receive, and a strict policy on unknown keys turns any drift between them into a drop or an exception at the worst possible moment. Tolerating the extra key, logging it, and reading the value from a field that the table does define keeps the panel working and keeps the drift visible. serpbase.dev