Skip to content

Results and models

Every call returns a Pydantic model, not a dictionary. The models map the API's JSON keys to typed attributes, fill defaults for the values that a record omits, and keep the raw payload reachable when you need it. This page shows what you get back from a call, what the models parse for you, and how to serialize a result.

What a call returns

The first block is a complete script. Later snippets on this page repeat the async with line so each one runs on its own; in your own code, put them inside one async with OpenAlexClient() as client: context like the first block does.

import asyncio

from bibliofabric.openalex import OpenAlexClient


async def main() -> None:
    async with OpenAlexClient() as client:
        work = await client.works.get("10.1038/nature12373")
        print("title:", work.title)
        print("year:", work.publication_year, "date:", work.publication_date)
        print("cited_by_count:", work.cited_by_count, "retracted:", work.is_retracted)
        print(
            "authors:",
            len(work.authorships),
            "first:",
            work.authorships[0].author.display_name,
        )


asyncio.run(main())
title: Nanometre-scale thermometry in a living cell
year: 2013 date: 2013-07-31
cited_by_count: 2198 retracted: False
authors: 8 first: Georg Kucsko

What the models parse for you

The attribute names are stable even where the APIs differ: Crossref spells the citation count is-referenced-by-count, and the model exposes it as is_referenced_by_count. Values the API sends as nested objects arrive as models, and repeated values arrive as lists of models, such as the eight authorships entries above. Boolean flags are parsed once in the model, so work.is_retracted is False, never the string "false" or a missing key.

Most date fields are strings exactly as the API sends them (work.publication_date is "2013-07-31"). The few fields that a model types as datetime, such as link_publication_date on OpenAIRE Scholix links, come back as datetime objects.

Two shared types absorb upstream messiness. Fields typed SafeStr turn a missing value into "" instead of None, and fields typed SafeList turn None into [] and a scalar into a one-element list, so a record that lists one author as a string still validates into a list.

Optional fields hold None

Fields a record can lack, and that no Safe type covers, hold None. The OpenAlex work above lists an article processing charge estimate (apc_list), but OpenAlex has no record of what was actually paid, so apc_paid is None. Check before you chain attributes:

async with OpenAlexClient() as client:
    work = await client.works.get("10.1038/nature12373")
    print("apc_paid:", work.apc_paid)
    print("venue:", work.primary_location.source.display_name)
apc_paid: None
venue: Nature

Page.meta keeps the raw envelope

search() returns a Page with .results, .total, .next_cursor, and .page. The API's response envelope, minus the results, is kept in .meta, so extras such as facets or query timing stay reachable.

async with OpenAlexClient() as client:
    page = await client.works.search({"publication_year": 2024}, page_size=2)
    print("total:", page.total)
    print("meta keys:", sorted(page.meta.keys()))
total: 10944043
meta keys: ['cost_usd', 'count', 'db_response_time_ms', 'group_by', 'groups_count', 'page', 'per_page', 'x_query']

Paging itself is covered in Page through results.

Serialize a result

model_dump() returns the record as a plain dictionary of Python values, ready for json.dumps, pandas, or a database row. Pass mode="json" when you need JSON-serializable values for dates and other types.

async with OpenAlexClient() as client:
    work = await client.works.get("10.1038/nature12373")
    dump = work.model_dump()
    print("keys:", len(dump), "| doi:", dump["doi"])
keys: 62 | doi: https://doi.org/10.1038/nature12373

Where the raw JSON lives

When a payload fits the model, the parsed result is all you see; the models are typed, so a typo in an attribute name fails loudly at your call site. When a payload does not fit, the call raises ValidationError, and the untouched JSON is on the exception as .raw. That path, including how to skip bad records during a harvest, is in Errors, retries, and rate limits.

Some clients also expose the untouched response on the model for routes that answer richer data than the model covers: an ISSN Portal record, for example, keeps the JSON-LD node in record.raw. The models themselves are documented per client in its API reference, for example OpenAlex and Crossref.