Next-step decision
from openprogram import Agent, decision
class ExampleAgent(Agent):
method_options = {'route_message': {'tool': True}}
def route_message(self, msg: str) -> str:
return decision.make("Pick one way to handle this message.", {
"analyze": analyze_sentiment, # a function
"fallback": fallback_reply, # a function
"done": "CONVERSATION_OVER", # a value
})
The model picks a function: it runs with parsed and injected arguments and its return value comes back. The model picks a value: that value comes back. The decision is the branch; there is no if.
Work first, decide last
from openprogram import Agent, agent
class ExampleAgent(Agent):
method_options = {'handle_ticket': {'tool': True}}
def handle_ticket(self, ticket: str) -> dict:
"""Read the ticket, look things up, then decide which flow to route to."""
return agent(
f"Handle this ticket: {ticket}",
toolset="default", # before: research with tools, run commands
choices={ # closing: the return must be one of these
"refund": issue_refund,
"escalate": escalate_to_human,
"close": {"status": "closed"},
},
)
decision.make(prompt, options) | agent(prompt, choices=options) | |
|---|---|---|
| Model does | Only picks | A full turn with reasoning and tools from tools or toolset; its final reply must be one {"call": ...} JSON |
| Prompt | Your prompt and the menu; your prompt must ask for a pick | Menu and DECISION_FINISH_INSTRUCTION spliced in |
| Retries | max_retries, default 1, 0 disables | Fixed at one |
Without choices, agent returns the reply text. Inside an Agent method both use the enclosing call's Runtime. Outside any Agent call pass runtime=; decision.make otherwise raises RuntimeError. Code: openprogram/runtime/agent/decision.py.
Versus native tool calls
| Native tool call | Next-step decision | |
|---|---|---|
| Options reach the model by | The protocol's tools field | Text menu in the prompt |
| Pick expressed as | Structured ToolCall | JSON in the reply body |
| Parsed by | Provider and agent loop | decision.py |
| An option can be a value | No | Yes, typically done or escalate |
| Needs | Provider tool use | Plain text |
One Agent method can serve both as a native tool and as an option. Also weigh whether every decision and retry should be a traceable DAG node.
Options
| Kind | When picked | Schema from |
|---|---|---|
| Function | Runs it, returns its return value | Function signature |
| Value | Returns the fixed value | None |
| Schema | Model fills the schema; returns {"decision": name, **fields} | The schema you declare |
decision.make("...", {
"retry": retry_fn, # function option
"skip": "SKIPPED", # value option
"abort": (AbortSignal(), "pick when stuck"), # value option + description
"emit_plan": ("Produce a plan.", { # schema option: ("description", schema)
"steps": [{"action": str, "target": str}],
"rationale": str,
}),
})
Dict form: {name: handler}. List form: a callable, (callable, "description"), "name", ("name", "description") or ("name", "description", schema); a function's name is its __name__.
Schema notation
| Written as | Means |
|---|---|
field: str (any Python type) | Scalar of that type |
field: "description" | Described str scalar |
field: [subschema] | List of exactly one item template; [str, int] raises TypeError |
field: {subfield: ...} | Nested object, needing at least one key outside the meta-spec set |
field: {"type": T, "description": ..., "options": [...]} | Meta-spec. A dict whose keys all fall in {type, description, options, fields, items} is read this way. |
Tuples are reserved in handler position: a literal 2-tuple value is misread as (value, "description"). To always return one structure, use a single-option decision.make or exec(response_format=...).
Internal steps
render_optionsemits each option's signaturename(param: type, ...), description, per-parameter detail withoptions, and a one-lineCall:JSON example with native placeholders (0,false,[],{},"<str>") and nested shape. Onlysource="llm"parameters show.- Model call.
decision.makesendsruntime.exec(prompt + menu);exec(choices=)splices the menu into its turn. parse_argschecks the reply as in the table below and returns(chosen, kwargs): the function, or the name string.- Retry on
_ParseError: the previous reply, the reason and the re-rendered menu go back throughruntime.exec, recorded in the DAG like any model call. When exhausted:DecisionError, with the last error kind, message and head of the reply. It subclassesValueError; there is no built-in fallback. resolve_decisionrunschosen(**kwargs), or looks up the value; a value option with a schema returns{"decision": name, **kwargs}.
| parse_args step | Behaviour |
|---|---|
extract_action | Finds the JSON with a call key (aliases action, function, tool) in a JSON code block or bare text |
| Unknown name | _ParseError("unknown_call") |
_validate_field | Checks type (str/int/float/bool/list/dict; bool is not int, float accepts int), enum and nesting |
| Function options | source="context" parameters come from the context dict, runtime-class parameters are injected, unknown fields dropped, required ones checked |
| Value and schema options | Every declared field required, others dropped |