pydantic/pydantic · error · TypeError

Unknown content-type: {content_type}

Error message

Unknown content-type: {content_type}

What it means

Raised as `TypeError` by `load_str_bytes` when a `content_type` is supplied but does not end with `json`/`javascript` and (with `allow_pickle`) not `pickle`. It is pydantic v1's way of saying it cannot infer the serialization protocol from the given MIME/content-type.

Source

Thrown at pydantic/v1/parse.py:30

    pickle = 'pickle'


def load_str_bytes(
    b: StrBytes,
    *,
    content_type: str = None,
    encoding: str = 'utf8',
    proto: Protocol = None,
    allow_pickle: bool = False,
    json_loads: Callable[[str], Any] = json.loads,
) -> Any:
    if proto is None and content_type:
        if content_type.endswith(('json', 'javascript')):
            pass
        elif allow_pickle and content_type.endswith('pickle'):
            proto = Protocol.pickle
        else:
            raise TypeError(f'Unknown content-type: {content_type}')

    proto = proto or Protocol.json

    if proto == Protocol.json:
        if isinstance(b, bytes):
            b = b.decode(encoding)
        return json_loads(b)
    elif proto == Protocol.pickle:
        if not allow_pickle:
            raise RuntimeError('Trying to decode with pickle with allow_pickle=False')
        bb = b if isinstance(b, bytes) else b.encode()
        return pickle.loads(bb)
    else:
        raise TypeError(f'Unknown protocol: {proto}')


def load_file(
    path: Union[str, Path],

View on GitHub (pinned to 2e5f0e2b42)

Solutions

  1. Pass the protocol explicitly via `proto=Protocol.json` instead of relying on content_type.
  2. If you genuinely have a non-JSON payload, pre-decode it yourself and pass the Python object.
  3. Correct or drop the content_type argument so it is one of json/javascript (or pickle with allow_pickle=True).

Example fix

// before
from pydantic.v1.parse import load_str_bytes
load_str_bytes(b'...', content_type='application/xml')

// after
from pydantic.v1.parse import load_str_bytes, Protocol
load_str_bytes(b'...', proto=Protocol.json)
Defensive patterns

Strategy: validation

Validate before calling

from pydantic.v1.parse import Protocol
_KNOWN = ('json', 'javascript', 'pickle')
def content_type_ok(ct: str, allow_pickle: bool) -> bool:
    return ct.endswith(('json', 'javascript')) or (allow_pickle and ct.endswith('pickle'))
# assert content_type_ok(content_type, allow_pickle) before calling

Try / catch

except TypeError:
    # unsupported content_type; fall back to explicit proto=Protocol.json

Prevention

When it happens

Trigger: Calling `parse_obj_as` / `load_str_bytes` / `load_file` with `content_type` set to something pydantic does not recognize (e.g. `content_type='application/xml'`, `content_type='msgpack'`, or a typo like `content_type='jso'`).

Common situations: Feeding data tagged with a custom or upstream content-type, mis-typing the content-type string, or assuming pydantic supports arbitrary MIME types for deserialization.

Related errors


AI-assisted analysis of pydantic/pydantic@2e5f0e2b42 (2026-08-04). Data as JSON: /data/errors/3b570cf569129174.json. Report an issue: GitHub.