Skip to content

Breaking: run stateful Strands models with their server-side state - #196

Merged
campoy merged 3 commits into
mainfrom
fix/strands-python-stateful-responses-mode-ignored
Oct 28, 2026
Merged

campoy merged 3 commits into
mainfrom
fix/strands-python-stateful-responses-mode-ignored

Conversation

@campoy

@campoy campoy commented Oct 26, 2026 •

Copy link
Copy Markdown
Contributor

Builds on #194, now on main: it reads the model's stateful flag through #194's config Activity.

What was changed

A worker model that keeps the conversation server-side, such as
OpenAIResponsesModel(stateful=True), now works through TemporalAgent as it does natively. Before,
TemporalModel.stateful was always False, so the agent kept and resent its whole history, and the
model Activity never got the agent's model_state, so no request carried previous_response_id.

  • The config Activity from Breaking: give TemporalAgent the worker model's context window and model id #194 also returns the model's stateful flag, and
    TemporalModel.stateful reports it.
  • A stateful model's calls run as their own Activities, strands_invoke_stateful_model and
    strands_invoke_stateful_model_streaming. They take a copy of the agent's model state, pass it to
    the model, and return the events with the updated state, which TemporalModel copies back. A
    Worker 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_model and its plain list
    result. The stateful calls go through the same context window overflow handling as invoke_model,
    and their events carry bytes in the same tagged form as its events (Carry image, document and redacted reasoning bytes across the Strands model and MCP activities #150).
  • Once TemporalAgent reads that the model is stateful, it does what Strands' Agent does at
    construction: it raises Strands' ValueError if a conversation_manager or context_manager
    was given, and otherwise uses NullConversationManager. Strands then clears the agent's messages
    after 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=...) and TemporalAgent.model_state carry the model state across
    Continue-As-New.
  • README: a paragraph on stateful models and their rollout, and the model state in the
    Continue-As-New example.

Breaking change: an agent given a conversation_manager or context_manager whose worker model
is stateful now raises ValueError at its first invocation (natively, at construction). Code that
read 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.messages starts the model's conversation over: also
carry agent.model_state, as the README shows. The title starts with Breaking: because release
notes 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 stateful reads as False, so runs recorded with #194 alone take the same
commands 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 config
Worker 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

  1. Closes [Bug] strands_agents: a stateful OpenAIResponsesModel runs statelessly through the plugin #195

  2. How was this tested:

  3. Any docs updates needed?
    The README covers stateful models, their rollout, and the model state in the Continue-As-New
    example.

Not covered:

Open questions:

  • The agent learns the model is stateful at its first invocation, so the ValueError for a supplied
    manager comes then, not at construction as natively. Is that acceptable?
  • Should a carried non-empty model_state count as stateful when the config does not say, so a
    successor on an older Worker waits for an upgraded one instead of starting over?

Draft while these questions are open.

Base automatically changed from fix/strands-python-model-context-window-hidden to main October 28, 2026 21:47
@campoy
campoy force-pushed the fix/strands-python-stateful-responses-mode-ignored branch from d6a1b8e to 28cb6df Compare October 28, 2026 21:48
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.
@campoy
campoy force-pushed the fix/strands-python-stateful-responses-mode-ignored branch from 28cb6df to f3a26b8 Compare October 28, 2026 21:49

@campoy campoy left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@campoy
campoy marked this pull request as ready for review October 28, 2026 22:21
@campoy
campoy requested a review from a team as a code owner October 28, 2026 22:21
@campoy
campoy merged commit ffda4f6 into main Oct 28, 2026
15 checks passed
@campoy
campoy deleted the fix/strands-python-stateful-responses-mode-ignored branch October 28, 2026 22:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] strands_agents: a stateful OpenAIResponsesModel runs statelessly through the plugin

2 participants