Skip to content

Python: [Bug]: RedisHistoryProvider max_messages trimming can orphan a tool result, causing every subsequent turn to fail #9851

Closed

Description

Observed Behavior

When RedisHistoryProvider is configured with max_messages and the agent uses tools, a conversation can become permanently unusable after a few turns.

save_messages enforces the limit with LTRIM key -max_messages -1. This trim does not account for tool call/result pairs, so it can remove an assistant message containing a function_call while keeping the following tool message with its function_result. The stored history then begins with a tool result that has no preceding call.

On the next turn, this history is sent to the model and the request is rejected:

  • Chat Completions: messages with role 'tool' must be a response to a preceeding message with 'tool_calls'.
  • Responses API with store=False: No tool call found for function call output with call_id ...

Because the failed turn is not persisted, the stored history never changes and every subsequent turn fails with the same error, including unrelated messages. The only recovery I found is clear(session_id), which discards the entire conversation.

This occurs with common limits. Running the same 10-turn conversation (a mix of tool calls and plain messages) with Chat Completions, limits of 4, 6, 8, 10, 12 and 20 all failed permanently; 3, 5 and 7 completed only because the trim boundaries happened to fall on safe positions. With approval-required tools, every limit from 3 to 8 failed, since the stored list can also begin with a function_approval_response followed by the orphaned result. With the Responses client's default store=True, the error did not occur.

Expected Behavior

Trimming should always leave a valid history. A conversation with a message limit should drop older messages while continuing to work.

Steps to Reproduce

  1. Start Redis: docker run -d --rm -p 127.0.0.1:6379:6379 redis:8.0.3
  2. Run the script below with OPENAI_API_KEY set.
  3. Turns 1 and 2 succeed. After turn 2, the stored list begins with a tool message, and turns 3, 4 and 5 all fail with a 400 error.

The same behavior is visible in DevUI with this agent: every message from turn 3 onward fails. The broken history stays in Redis, so the same session keeps failing even after a restart.

Minimal Reproduction

import asyncio
import uuid
from typing import Annotated

from agent_framework import Agent, tool
from agent_framework.openai import OpenAIChatCompletionClient
from agent_framework.redis import RedisHistoryProvider


@tool
def get_weather(city: Annotated[str, "City name"]) -> str:
    """Return the weather for a city."""
    return f"{city}: sunny, 21C"


async def main() -> None:
    agent = Agent(
        client=OpenAIChatCompletionClient(model="gpt-4.1-mini"),
        instructions="Be very brief. Always use get_weather for weather questions.",
        tools=[get_weather],
        context_providers=[
            RedisHistoryProvider(redis_url="redis://localhost:6379", application_id="repro", max_messages=6)
        ],
    )
    session = agent.create_session(session_id=f"repro-{uuid.uuid4().hex[:6]}")
    for i, text in enumerate(["What's the weather in Paris?", "And in Rome?", "Thanks!", "Hello?", "What is 2+2?"], 1):
        try:
            response = await agent.run(text, session=session)
            print(f"turn {i}: OK {response.text!r}")
        except Exception as exc:
            print(f"turn {i}: {type(exc).__name__}")


asyncio.run(main())

Output:

turn 1: OK 'The weather in Paris is sunny with a temperature of 21°C.'
turn 2: OK 'The weather in Rome is sunny with a temperature of 21°C.'
turn 3: ChatClientException
turn 4: ChatClientException
turn 5: ChatClientException

Stored roles after turn 2: tool, assistant, user, assistant (function_call), tool, assistant.

Error Messages and Stack Traces

openai.BadRequestError: Error code: 400 - {'error': {'message': "Invalid parameter: messages with role 'tool' must be a response to a preceeding message with 'tool_calls'.", 'type': 'invalid_request_error', 'param': 'messages.[1].role', 'code': None}}

Responses API with store=False:
Error code: 400 - {'error': {'message': 'No tool call found for function call output with call_id call_...', 'type': 'invalid_request_error', 'param': 'input', 'code': None}}

Package Versions

agent-framework-core 1.23.0, agent-framework-redis 1.0.0b261022, agent-framework-openai 1.17.0, redis 7.1.1, openai 3.36.1

Python Version

Python 3.13.7 (also reproduced on 3.12.15)

Operating System

macOS

Regression

No

Additional Context

The trim is in python/packages/redis/agent_framework_redis/_history_provider.py, in save_messages:

if self.max_messages is not None:
    current_count: int = await _redis_result(self._redis_client.llen(key))
    if current_count > self.max_messages:
        await _redis_result(self._redis_client.ltrim(key, -self.max_messages, -1))

This behavior has existed since the provider was introduced in #3763, and the previous RedisChatMessageStore trimmed the same way.

Proposed fix: in get_messages, skip messages at the start of the loaded list that are tool messages or contain only function_result / function_approval_response content. Because the fix applies on read, it also restores sessions that are already affected, without modifying data in Redis. With this change, all of the cases above complete successfully, and each approved tool still executes exactly once. I added 4 unit tests (3 fail on current main), and the redis package tests, the full Python test suite and the typing checks pass.

One behavioral note: callers that read get_messages directly will no longer see those leading items. The duplicate detection in save_messages, which also calls get_messages, is unaffected; I compared the stored data with and without the change and it was identical.

An alternative is to trim on complete call/result groups in save_messages. On its own, however, that would not recover sessions that are already affected. Since this change touches function-call history, I will wait for the team to confirm the preferred approach before opening a pull request.

AI tools assisted with the investigation, the reproduction script and the drafting of this issue. I ran the reproduction and verified the results myself.

I have a fix ready and can open a PR once the approach is confirmed.

Acknowledgements

  • I searched existing issues and did not find a duplicate.
  • I personally verified this behavior and the reproduction details are authentic.
  • I will wait for explicit maintainer agreement before starting implementation of a non-trivial change.

Activity

  1. added
    pythonUsage: [Issues, PRs], Target: Python
    triageUsage: [Issues], Target: All issues that still need to be triaged
    on Oct 29, 2026
  2. added
    reproducedUsage: [Issues], Target: all issues that can be reproduced by the triage workflow
    on Oct 29, 2026
  3. github-actions commented on Oct 29, 2026

    @github-actions
    Contributor

    🤖 Automated triage reproduction notes (agent-authored — trust but verify)

    Agent analysis

    python/packages/redis/agent_framework_redis/_history_provider.py::RedisHistoryProvider.save_messages at lines 318-321 produces invalid history when max_messages trims between an assistant function call and its tool result; get_messages then returns the orphan unchanged. Minimal repro: save two four-message user/call/result/assistant turns with max_messages=6, then verify the loaded history begins with a tool message.

    • Failing test: python/packages/redis/tests/test_providers.py
    • Files examined: python/packages/redis/agent_framework_redis/_history_provider.py, python/packages/redis/tests/test_providers.py, python/packages/redis/pyproject.toml, docs/specs/004-python-function-calling-loop.md
    • Tests run: existing max_messages trim test (passed), orphaned function-result regression test (failed), remaining provider tests (67 passed)
    • Reported version: 1.0.0b261022
    • Current version: 1.0.0b261022
  4. added
    memoryUsage: [Issues, PRs], Target: memory related features
    and removed
    triageUsage: [Issues], Target: All issues that still need to be triaged
    on Oct 29, 2026
  5. eavanvalkenburg commented on Nov 2, 2026

    @eavanvalkenburg
    Member

    /plan Plan the remaining RedisHistoryProvider history-retention and recovery contract for the confirmed orphan-result failure on current main, using active #9890 and its latest revisions/discussion as the implementation baseline; do not start a parallel fix. The PR author now reports fixes in ab94c48/3b4452713 for the earlier approval identity, occurrence pairing, watched-connection and retry-exhaustion feedback, so verify those current revisions rather than repeat stale findings. Resolve the issue original read-time recovery proposal versus write-time self-contained-window retention: whether/how already-corrupt sessions recover, strict versus group-aware max_messages behavior, unmatched/pending calls, approval authority and nonadjacent/reused-ID pairing. Cross-check the stable function-calling-loop contract and preserve exactly-once execution. Validate persistence concurrency with watched-connection reads, borrowed single-connection clients and explicit failure after retry exhaustion, plus approved/local/hosted call groups, max_messages=0 and existing invalid histories. Keep the existing PR open and current assignment/labels unchanged while policy and migration behavior are settled; no ready label or new assignment.

  6. LUCKYREDDY31 commented on Nov 2, 2026

    @LUCKYREDDY31
    Author

    I tested #9890 at its current head (3b44527) against main 60eae45, with real Redis 8.0.3 and gpt-6-luna. I used RedisHistoryProvider(max_messages=N) with a tool, on Chat Completions (reasoning_effort="none", which gpt-6-luna needs for tools there) and on Responses with store=False. This is meant to help with the open points in the plan above, so I checked the latest revisions rather than the earlier review findings.

    The latest review fixes hold in live runs

    • Saving through a borrowed single_connection_client works.
    • When the key keeps changing, the save now raises WatchError after the retries instead of reporting success.
    • With approval-required tools, the retained approval request/response sequences work at limits 3 to 8, and each approved tool ran exactly once.

    New sessions: #9890 fixes them
    On main, a 10-turn chat breaks permanently at limits 4, 6, 8 and 10, on both APIs. With #9890 there were no failures at any of those limits.

    Sessions corrupted before the upgrade: #9890 alone does not recover them
    To test this, I broke a session on main (the stored list starts with an orphaned tool result), switched to #9890, and sent the next message. It fails with the same 400, and the stored list does not change. This matches the stated design ("the trim is responsible for not creating orphans, not for repairing history damage"). The orphan is loaded by the unchanged get_messages, and the turn fails before it reaches save_messages, so the new trimming never runs for that session. This held in every shape I tried: Chat Completions with and without streaming, Responses store=False with and without streaming, and approval tools at limits 3, 5 and 8.

    Adding the read-time skip from the issue on top of #9890 recovers them

    A pending-call case the read-time skip does not cover
    If a session broke in the middle of an approval, the stored list can also end with a function_call + function_approval_request that was never answered (seen at max_messages=5). The first model call after the skip correctly omits it. But on the next approval round, that stale call is sent without a result and fails with "An assistant message with 'tool_calls' must be followed by tool messages..." (4 of 4 trials). This seems to belong to the unmatched/pending call question in the plan.

    Concurrency
    With _SAVE_WATCH_RETRIES = 8, simultaneous saves to the same session never failed at 2, 3 or 5 at a time. They started failing at 10 at a time (9 of 50 saves raised WatchError) and 20 at a time (5 of 20). In the same test, main stays "successful" but leaves an orphan at the head of the list.

    Other checks

    • poe check -P redis and poe test-typing -P redis pass.
    • Tests pass on Python 3.11 to 3.14 and with redis-py 6.4.0 and 7.1.1.
    • max_messages=0 and None behave as before.
    • Minor: the new tests emit 17 "coroutine ... was never awaited" warnings at pipe.multi(). Pipeline.multi() is synchronous in redis-py 6.4.0 and 7.1.1, so this comes from the pipeline being mocked as an AsyncMock, not from the code.

    I'm not opening a separate PR. Happy to share the 11-line patch if it helps for #9890.

    AI tools assisted with the testing and with drafting this comment. I ran the tests and checked the results myself.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

memoryUsage: [Issues, PRs], Target: memory related featurespythonUsage: [Issues, PRs], Target: PythonreproducedUsage: [Issues], Target: all issues that can be reproduced by the triage workflow

Type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions