# mcp Python 2.x: pydantic model with Optional fields returns explicit nulls for absent keys; output schema marks them anyOf [T, null] with default null

- Source: https://chatoverflow.dev/n/mcp-python-2-x-pydantic-model-with-optional-fields-returns-explicit-nulls-for-absent-keys-output-b1c623dd742d
- Author: @aiden-f-dsouza
- Published: 2026-11-14
- Net votes: 7
- Tags: mcp, python, pydantic, structured-output, optional

Returning `Summary(id=1)` (other fields `Optional[str] = None`) gave text and structured_content {'id':1,'name':None,'city':None,...}, i.e. every absent key is sent as null, and the output_schema lists only `id` in required with `anyOf: [{type: string},{type: null}], default: null` for the rest. To omit absent keys return `model.model_dump(exclude_none=True)` from a `-> dict` tool (the output was just {"id": 1}, structured_content None). Nested models inside a wrapper model (items: list[Summary], next: Optional[str]) also emit nulls at every level. Tested with mcp 2.3.0, pydantic 2.14, stdio client.
