Repository navigation
Breaking: run stateful Strands models with their server-side state - #196
Conversation
d6a1b8e to
28cb6df
Compare
A worker model with server-side conversation state, such as OpenAIResponsesModel(stateful=True), ran statelessly through the plugin: TemporalModel.stateful was always False, so the agent kept and resent its local history, and the model activity never got the agent's model state, so requests had no previous_response_id. The model config activity now also returns the model's stateful flag, and TemporalModel reports it. The model activity takes the agent's model state when the model is stateful and returns the updated state with the events; TemporalModel copies it back into the agent's state. Calls without state keep the plain list result, so histories recorded before this change still read as stateless. Once TemporalAgent learns the model is stateful, it does what Strands does at construction: it uses NullConversationManager, and Strands then clears the agent's messages after each turn. Breaking change: a TemporalAgent given a conversation_manager or context_manager now raises ValueError at its first invocation when its worker model is stateful, as Strands' Agent does at construction. Remove the manager from those agents. Code that read the agent's messages after a turn with a stateful model now finds them cleared, as natively. This builds on the model config activity: every run reads the model's stateful flag before its first invocation.
Replace the first version, which sent the model state through invoke_model. During a rolling upgrade a Worker without stateful support could take that call, drop the state and return a plain event list, while the agent, told the model is stateful, still cleared its messages, so the conversation was lost. Stateful calls now run as strands_invoke_stateful_model and strands_invoke_stateful_model_streaming, which such a Worker does not register: it fails the call and Temporal retries it until an upgraded Worker takes it. Stateless calls keep invoke_model and its plain list result. TemporalAgent(model_state=...) and the TemporalAgent.model_state property carry the model state, such as the last response id, across Continue-As-New; the README's chat example carries it with the messages. The README also says to upgrade every Worker on the task queue, or use Worker Versioning, and that a run continued as new on a Worker without this version starts the model's conversation over.
28cb6df to
f3a26b8
Compare
campoy
left a comment
There was a problem hiding this comment.
There's a backwards compatibility issue, but we agreed that's ok and we don't need to worry. The instructions and release notes should mention that workflows should be drained before upgrading.
| self._config["model_id"] = result.model_id | ||
| if result.context_window_limit is not None: | ||
| self._config["context_window_limit"] = result.context_window_limit | ||
| self._stateful = result.stateful |
There was a problem hiding this comment.
Clarify the mixed-version upgrade limits
With backward compatibility outside scope, this is an upgrade documentation point. During a rolling upgrade from #194, an old workflow worker can receive stateful: true from an upgraded config activity worker, ignore it, and record invoke_model. Replaying that history on this build schedules strands_invoke_stateful_model and fails with [TMPRL1100].
Please make explicit that mixed-version histories are also outside the compatibility promise. Existing runs should finish before the upgrade or stay on isolated worker versions, including their config activities.
Builds on #194, now on
main: it reads the model'sstatefulflag through #194's config Activity.What was changed
A worker model that keeps the conversation server-side, such as
OpenAIResponsesModel(stateful=True), now works throughTemporalAgentas it does natively. Before,TemporalModel.statefulwas alwaysFalse, so the agent kept and resent its whole history, and themodel Activity never got the agent's
model_state, so no request carriedprevious_response_id.statefulflag, andTemporalModel.statefulreports it.strands_invoke_stateful_modelandstrands_invoke_stateful_model_streaming. They take a copy of the agent's model state, pass it tothe model, and return the events with the updated state, which
TemporalModelcopies back. AWorker without stateful support does not register them, so during a rolling upgrade it fails such
a call and Temporal retries it on an upgraded Worker, instead of running the model without its
state while the agent clears its messages. Stateless calls keep
invoke_modeland its plain listresult. The stateful calls go through the same context window overflow handling as
invoke_model,and their events carry
bytesin the same tagged form as its events (Carry image, document and redacted reasoning bytes across the Strands model and MCP activities #150).TemporalAgentreads that the model is stateful, it does what Strands'Agentdoes atconstruction: it raises Strands'
ValueErrorif aconversation_managerorcontext_managerwas given, and otherwise uses
NullConversationManager. Strands then clears the agent's messagesafter each turn. This runs right after Breaking: give TemporalAgent the worker model's context window and model id #194's config read, inside Strands' invocation guard.
TemporalAgent(model_state=...)andTemporalAgent.model_statecarry the model state acrossContinue-As-New.
Continue-As-New example.
Breaking change: an agent given a
conversation_managerorcontext_managerwhose worker modelis stateful now raises
ValueErrorat its first invocation (natively, at construction). Code thatread the agent's messages after a turn with a stateful model now finds them cleared, as natively, so
a Continue-As-New that carries only
agent.messagesstarts the model's conversation over: alsocarry
agent.model_state, as the README shows. The title starts withBreaking:because releasenotes come from commit subjects.
Replay and rollback: no version gate, here or in #194. Runs whose agents ran before #194 fail
replay on its config read, as #194 says; a run that used a stateful model before this change is one
of them. Model results recorded before this change are plain lists and read as stateless, and a
config result without
statefulreads asFalse, so runs recorded with #194 alone take the samecommands here. A build without stateful support cannot replay a stateful run: #194 alone fails on
the stateful Activity type with
[TMPRL1100]. A run that continues as new onto a build or configWorker without this change treats the model as stateless and starts its conversation over; the README
says to keep such runs on upgraded Workers.
Upgrade: let runs whose agents already ran finish on the earlier version before you upgrade, as
for #194, or keep them there with Worker Versioning. Do not roll back while stateful runs are open;
keep them on this version with Worker Versioning, or let them finish.
Why?
See #195.
Checklist
Closes [Bug] strands_agents: a stateful OpenAIResponsesModel runs statelessly through the plugin #195
How was this tested:
tests/test_model.py: two turns with a stateful mock model, with and withoutstreaming, and the refused manager; a Worker without the stateful Activities, where the call
waits for an upgraded Worker instead of dropping the state; and the README's Continue-As-New
handoff. They fail on
mainand pass with the change.ruff check --select I,ruff format --check,pyright,mypy,basedpyrightandpydocstylepass, and the full suite passes (135 tests). These ran with Python 3.13 and uv0.13.1, outside the plugin's
required-version, so not throughmake. CI coversmake lintand
make test.mainand Breaking: give TemporalAgent the worker model's context window and model id #194's branch, and none with thechange: the second request carries
previous_response_idand only the new message, and theagent has 0 messages after each turn.
this change and not rerun since:
expected;
[TMPRL1100](control).until an upgraded Worker takes it, then chains to the last response.
state keeps the response id.
Any docs updates needed?
The README covers stateful models, their rollout, and the model state in the Continue-As-New
example.
Not covered:
main, from Breaking: give TemporalAgent the worker model's context window and model id #194 and fromthis change were not replayed again.
the model's conversation over (see the README). The fix does not guard it.
temporalio1.34.x still bundles the old code astemporalio.contrib.strands. No backport here.Open questions:
ValueErrorfor a suppliedmanager comes then, not at construction as natively. Is that acceptable?
model_statecount as stateful when the config does not say, so asuccessor on an older Worker waits for an upgraded one instead of starting over?
Draft while these questions are open.