Skip to content

fix(agent): preserve structured output before history offloading - #5429

Merged
mkmeral merged 2 commits into
strands-agents:mainfrom
xxwtiancai:agent-tasks/5207
Oct 28, 2026
Merged

mkmeral merged 2 commits into
strands-agents:mainfrom
xxwtiancai:agent-tasks/5207

Conversation

@xxwtiancai

@xxwtiancai xxwtiancai commented Oct 22, 2026 •

Copy link
Copy Markdown
Contributor

Human Overview

Summary

Fix an issue where large agent responses get truncated by context management before being returned to the caller.

The Problem

When the agent generates a valid but lengthy response, context management compresses it into a short preview for history. The app then pulls the final result from this pruned history, receiving an incomplete answer and often triggering unnecessary retry loops or degraded returns.

The Fix

Decouple conversation context from the final deliverable:

  • Stash the validated structured result before context compression rewrites the message.
  • Context pruning still operates normally—this does not disable compression or keep oversized payloads in the LLM window.
  • The caller gets the intact result, while conversation history stays lean.

Core concept: History summarization exists to save context, not to dictate what the program returns.

Verification

  • Full output returned without truncation
  • Malformed formats still trigger retries as expected
  • Tool termination rules remain intact
  • Tool result offloading functions as before
  • Tests: 723 targeted tests passed; full Node suite: 5,353 passed / 6 skipped (independent review complete).

Description

Context management can replace a successful structured-output tool result with a text preview before the agent returns it, losing the validated answer and triggering another model call. Capture that validated output before message-added hooks while preserving hook-requested end turns and history offloading.

Related Issues

Resolves #5207

Documentation PR

No documentation change is needed; this restores the existing structured-output behavior.

Type of Change

Bug fix

Testing

Offline regression: two cases fail on the original implementation and pass with the fix. The affected Agent file passes in Node and Chromium (141 tests). Related Node tests pass (723); full Node coverage passes (5353 passed, 6 skipped). Build, lint, formatting, type-check and browser bundle pass. The pre-push aggregate reports existing dependency advisories (3 high, 6 moderate); dependency files are unchanged. No live-provider integration tests were run.

  • I ran hatch run prepare

Additional Details

Validation and AI assistance

TypeScript-only change. Ran the repository's pre-push script, the affected Agent browser tests, and the offline MockMessageModel reproduction. The configured npm mirror does not support its audit endpoint; the official npm registry audit reports the existing advisories above. Full browser coverage, package smoke tests and real-provider integrations were not run.

AI assisted the investigation, implementation, tests and technical PR draft. The Human Overview above is the contributor's own text, preserved as provided. Update to its verification status: independent review by a fresh Astra xhigh agent is now complete with no actionable findings; it separately passed 312 focused tests and six offline edge probes. This fix concerns validated tool-based structured output. The human reading and understanding attestations have since been explicitly confirmed by the contributor and are checked in the checklist below.

Checklist

  • I have read the CONTRIBUTING document
  • I have reviewed and understand every line of code in this PR, including any generated by AI tools, and I can explain why it works
  • My change is focused and reasonably small; I have split unrelated work into separate PRs
  • I have added any necessary tests that prove my fix is effective or my feature works
  • I have updated the documentation accordingly
  • I have added an appropriate example to the documentation to outline the feature, or no new docs are needed
  • My changes generate no new warnings
  • Any dependent changes have been merged and published

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

@github-actions github-actions Bot added bug Something isn't working area-structured-output Related to the structured output api area-context Session or context related python Pull requests that update python code labels Oct 22, 2026
@xxwtiancai
xxwtiancai marked this pull request as ready for review October 23, 2026 16:39
@xxwtiancai
xxwtiancai requested a review from a team as a code owner October 23, 2026 16:39
@xxwtiancai

Copy link
Copy Markdown
Contributor Author

Marking this ready for review. One housekeeping note: this is a TypeScript-only change (touches only strands-ts/), but the auto-labeler applied the python label — could a maintainer swap it for typescript? Fork contributors can't edit labels on the base repo. Thanks!

@xxwtiancai

Copy link
Copy Markdown
Contributor Author

Hi maintainers — gentle nudge: the "Run integration tests" check on this PR has been sitting in waiting since Oct 22 (~4.5 days) and "Trigger Strands Review" since Oct 23, while pipelines on newer PRs have completed in the meantime. Could someone check whether these runs are queued normally, or re-trigger them if they're stuck? Happy to help with anything needed from my side. Thanks!

@github-actions github-actions Bot added complexity/low Touched functions have low cognitive complexity (<=10) size/xs labels Oct 28, 2026
@mkmeral mkmeral added typescript Pull requests that update typescript code and removed python Pull requests that update python code labels Oct 28, 2026
@mkmeral

mkmeral commented Oct 28, 2026

Copy link
Copy Markdown
Member

I've merged from main, because there were unrelated test failures fixed in main. Once the CI passes, PR should merge.

Thank you for the fix! :)

@mkmeral
mkmeral enabled auto-merge (squash) October 28, 2026 16:32
@mkmeral
mkmeral merged commit fa313cc into strands-agents:main Oct 28, 2026
27 of 28 checks passed
Unshure added a commit to strandly-the-agent/harness-sdk that referenced this pull request Oct 28, 2026
Reapplies two fixes from main that were dropped when the earlier
rebase of 31800e8 resolved via --theirs:

- fa313cc (strands-agents#5429) extract structuredOutput before the deferred
  append, so a message-added hook that offloads the JSON block
  can't lose it.
- a4865cd (strands-agents#5628) activate interrupt state before yielding
  InterruptEvents in the catch path, so a consumer that stops at
  an InterruptEvent can still resume.
Unshure added a commit to strandly-the-agent/harness-sdk that referenced this pull request Oct 28, 2026
Reapplies two fixes from main that were dropped when the earlier
rebase of 31800e8 resolved via --theirs:

- fa313cc (strands-agents#5429) extract structuredOutput before the deferred
  append, so a message-added hook that offloads the JSON block
  can't lose it.
- a4865cd (strands-agents#5628) activate interrupt state before yielding
  InterruptEvents in the catch path, so a consumer that stops at
  an InterruptEvent can still resume.
Unshure added a commit to strandly-the-agent/harness-sdk that referenced this pull request Oct 29, 2026
Reapplies two fixes from main that were dropped when the earlier
rebase of 31800e8 resolved via --theirs:

- fa313cc (strands-agents#5429) extract structuredOutput before the deferred
  append, so a message-added hook that offloads the JSON block
  can't lose it.
- a4865cd (strands-agents#5628) activate interrupt state before yielding
  InterruptEvents in the catch path, so a consumer that stops at
  an InterruptEvent can still resume.
gautamsirdeshmukh pushed a commit to gautamsirdeshmukh/harness-sdk that referenced this pull request Nov 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-context Session or context related area-structured-output Related to the structured output api bug Something isn't working complexity/low Touched functions have low cognitive complexity (<=10) size/xs typescript Pull requests that update typescript code

Projects

None yet

2 participants