#2726·instructor

Partial streaming fails for data-aware default factories across chunk boundaries

Author: Ytq1115Created Sep 30, 2026Updated Sep 30, 2026

What happens

Partial streaming raises for a valid Pydantic model whose default_factory takes the already validated values of earlier fields. The same JSON succeeds in a single chunk but fails when split before the final closing brace. Both synchronous and asynchronous chunk parsers are affected.

Reproduction

No model API call or credentials are needed:

python
from instructor import Partial
from pydantic import BaseModel, Field

class User(BaseModel):
    email: str
    username: str = Field(default_factory=lambda data: data["email"].split("@")[0])

payload = '{"email":"[email protected]"}'
print(User.model_validate_json(payload))
print(list(Partial[User].model_from_chunks([payload])))
print(list(Partial[User].model_from_chunks([payload[:-1], "}"])))

The first two operations succeed with username='alice'. The last raises:

ValueError: The default factory requires the 'validated_data' argument, which was not provided when calling 'get_default'.

Reproduced on current main (e12f8b49203b0c1f253d27c1e709d0a09b9fc5a8, package version 1.17.1), Python 3.12.14, Pydantic 2.11.7.

Expected behavior

Partial snapshots should preserve the available streamed fields and defer the dependent default until the containing object is validated. The completed result should match ordinary Pydantic validation, regardless of chunk boundaries. Ordinary literal defaults and zero-argument factories should remain available during streaming.

Cause and proposed fix

_build_partial_object() calls field_info.get_default(call_default_factory=True) for missing fields without supplying validated data. Supplying the partially constructed dictionary would also be incorrect: dependencies can still be missing, incomplete, or awaiting coercion and validators.

Defer single-required-argument factories during partial construction and let the existing completed-object validation evaluate them. Retain ordinary default copying and zero-argument factories. Also distinguish actual complete {} from the synthetic empty placeholder used before JSON arrives, so an all-defaulted model's completed result includes its dependent defaults.

I have a focused fix with sync/async regression tests ready.