coact.return_contract¶
Realizing a return contract — the backend-agnostic part of DECISIONS D6.
The return contract (an agent’s final-message JSON Schema) reaches a runtime two
ways, and the how is a cohesive, independently testable subsystem extracted
here out of the (large) coact.realize:
tool — a forced
return_resultMCP tool whoseinput_schemaIS the schema, plus a system-prompt instruction to call it. The structured result is recovered from the tool-use block. Used by thesdkbackend (older SDKs, or whenoutput_formatcan’t be honored).json — ask for a single JSON value conforming to the schema in the system prompt. Used by the provider-agnostic
litellmbackend (where structuredresponse_formatsupport varies).
This module owns: the in-process return-tool naming, the ReturnPlan value,
mode selection, the object-schema coercion the SDK tool needs, the two
instruction renderers (sharing one schema-fence helper — DRY across backends),
and the tool-use extraction. The SDK-specific wiring (building the in-process
MCP server, ClaudeAgentOptions) stays in coact.realize; the
provider-specific response parsing stays in coact.realize_litellm.
Module Attributes
In-process SDK MCP server + tool names for the forced return path. |
|
How Claude references the tool ( |
Functions
|
Coerce a return schema to a valid object |
|
Pick the return mode in |
|
Recover the forced |
|
True if |
|
The portable structured-output instruction: ask for JSON conforming to a schema (litellm). |
Append the "you MUST call return_result" instruction to a system prompt (sdk). |
Classes
|
How an agent's return contract is realized for the SDK (resolved once). |
- coact.return_contract.RETURN_TOOL_FULLNAME = 'mcp__coact_return__return_result'¶
How Claude references the tool (
mcp__<server>__<tool>).
- coact.return_contract.RETURN_TOOL_SERVER = 'coact_return'¶
In-process SDK MCP server + tool names for the forced return path.
- class coact.return_contract.ReturnPlan(mode, schema, unwrap_key=None, tool_fullname='')[source]¶
Bases:
objectHow an agent’s return contract is realized for the SDK (resolved once).
- coact.return_contract.as_object_schema(schema)[source]¶
Coerce a return schema to a valid object
inputSchemafor the return tool.The Agent SDK passes a dict
input_schemathrough unchanged only when it is an object schema with bothtype: objectandproperties; any other dict is misread as a{param: type}map (and mangled). So:An object schema with
properties→ passed through, no wrapping.A free-form object schema (
type: objectbut noproperties) → given an emptypropertieskey so the SDK passes it through verbatim. Crucially it is not wrapped: wrapping a free-form object underresultwould make a model’s own single-key{result: ...}output indistinguishable from the wrapper (a silent-collapse hazard).Any non-object schema (array / scalar) → wrapped under a
resultkey, with that key returned so extraction can unwrap it.
>>> as_object_schema({'type': 'object', 'properties': {'a': {}}})[1] is None True >>> as_object_schema({'type': 'object'}) # free-form object: passed through, not wrapped ({'type': 'object', 'properties': {}}, None) >>> as_object_schema({'type': 'array', 'items': {'type': 'string'}}) ({'type': 'object', 'properties': {'result': {'type': 'array', 'items': {'type': 'string'}}}, 'required': ['result']}, 'result')
- coact.return_contract.auto_return_mode(option_field_names)[source]¶
Pick the return mode in
auto: nativeoutput_formatif the SDK has it.>>> auto_return_mode({'system_prompt', 'output_format'}) 'output_format' >>> auto_return_mode({'system_prompt'}) # older SDK without the field 'tool'- Return type:
- coact.return_contract.extract_return_tool_input(messages, unwrap_key)[source]¶
Recover the forced
return_resulttool-use input from SDK messages, or None.Scans assistant messages for a tool-use block calling the return tool and returns its
input(the last one wins). When the schema was wrapped (a non-object return type), the singleresultkey is unwrapped.- Return type:
- coact.return_contract.is_return_tool(name)[source]¶
True if
namedenotes coact’s forced return tool (server-scoped).Matches the full MCP name, or any prefix variant scoped to coact’s own server (
…__coact_return__return_result). Deliberately does not match a barereturn_resultor areturn_resulton a different MCP server, so a user’s own same-named tool is never mistaken for the return contract.>>> is_return_tool('mcp__coact_return__return_result') True >>> is_return_tool('mcp__other_server__return_result') False >>> is_return_tool('Read') False- Return type: