coact._pydantic_schema

Synthesize a pydantic model class from a coact JSON-Schema return contract.

Needed only where a realization backend’s structured-output API wants a type rather than a JSON-Schema dict — currently just the crewai backend, whose Agent.kickoff(response_format=<class>) is class-only. The langgraph backend passes the canonical JSON-Schema dict straight into ToolStrategy/ProviderStrategy and never calls this.

json_schema_to_model() returns None (it never raises) for any schema it cannot represent faithfully — empty, non-object, or carrying $ref/anyOf/oneOf/allOf or nested objects — so the caller degrades to the portable in-prompt return-contract instruction (DECISIONS D6) instead of fabricating a lossy model. Pydantic is imported lazily (it ships with crewai on the real path), so importing this module is free and does not pull pydantic.

coact._pydantic_schema.json_schema_to_model(schema: dict, *, name: str = 'ReturnContract') type | None[source]

Flat JSON-Schema object -> a pydantic BaseModel subclass, else None.

Handles {"type": "object", "properties": {...}, "required": [...]} whose property types are scalars/array/object (string->``str``, integer->``int``, number->``float``, boolean->``bool``, array->``list``, object->``dict``, unknown->``Any``). A property absent from required becomes Optional with default None. Anything that cannot be represented faithfully (non-object root, no properties, or any $ref/anyOf/oneOf/nested-object-with-properties property) returns None so the caller relies on the prompt instruction. Never raises.

>>> json_schema_to_model({"type": "array", "items": {"type": "string"}}) is None
True
>>> json_schema_to_model({}) is None
True
>>> json_schema_to_model({"type": "object", "properties": {}}) is None
True