Skip to content

Python: fix: prevent repeating instructions in continued Responses API conversations - #3909

Merged
Eduard van Valkenburg (eavanvalkenburg) merged 3 commits into
microsoft:mainfrom
eavanvalkenburg:fix-3498-no-repeat-instructions
Feb 13, 2026
Merged

Python: fix: prevent repeating instructions in continued Responses API conversations#3909
Eduard van Valkenburg (eavanvalkenburg) merged 3 commits into
microsoft:mainfrom
eavanvalkenburg:fix-3498-no-repeat-instructions

Conversation

@eavanvalkenburg

@eavanvalkenburg Eduard van Valkenburg (eavanvalkenburg) commented Feb 13, 2026

Copy link
Copy Markdown
Member

Summary

Fixes #3498

This PR prevents instructions from being repeatedly sent to the OpenAI Responses API when continuing a conversation.

Changes

  • Modified _prepare_options() in packages/core/agent_framework/openai/_responses_client.py to prepend instructions only on the first turn (when no conversation_id/response_id exists).
  • When a conversation/response ID is present, instructions are skipped since they already exist in server-side conversation context.
  • Applies to both OpenAI and Azure Responses API clients (Azure inherits from OpenAI implementation).
  • Consolidated continuation regression tests into the existing responses suite file instead of a standalone test file.

Testing

  • Added/updated regression tests in packages/core/tests/openai/test_openai_responses_client.py covering:
    • First turn includes instructions.
    • Continuation with resp_ format ID skips instructions.
    • Continuation with conv_ format ID skips instructions.
    • First-turn then continuation behavior in a single conversation flow.
  • uv run ruff format packages/core/tests/openai/test_openai_responses_client.py
  • uv run ruff check packages/core/tests/openai/test_openai_responses_client.py
  • uv run python -m pytest -q packages/core/tests/openai/test_openai_responses_client.py -k "instructions_sent_first_turn_then_skipped_for_continuation or instructions_not_repeated_for_continuation_ids or instructions_included_without_conversation_id"

Behavior

Before: Instructions were sent with every API call, causing repeated system messages and extra token usage.

After: Instructions are sent only on the first turn. Subsequent turns with conversation/response IDs do not prepend instructions again.

…sations

- Instructions are now only prepended to messages on the first turn
- When conversation_id/response_id exists (continuation), instructions are skipped
- Covers OpenAI and Azure Responses API paths
- Adds regression tests for all continuation scenarios

Fixes microsoft#3498
Copilot AI review requested due to automatic review settings February 13, 2026 10:29
@markwallace-microsoft Mark Wallace (markwallace-microsoft) added the python Usage: [Issues, PRs], Target: Python label Feb 13, 2026
@github-actions github-actions Bot changed the title fix: prevent repeating instructions in continued Responses API conversations Python: fix: prevent repeating instructions in continued Responses API conversations Feb 13, 2026
@markwallace-microsoft

Mark Wallace (markwallace-microsoft) commented Feb 13, 2026

Copy link
Copy Markdown
Contributor

Python Test Coverage

Python Test Coverage Report •
FileStmtsMissCoverMissing
packages/core/agent_framework/openai
   _responses_client.py6137987%294–297, 301–302, 305–306, 312–313, 318, 331–337, 358, 366, 389, 552, 555, 610, 614, 616, 618, 620, 696, 706, 711, 754, 831, 848, 861, 917, 1010, 1015, 1019–1021, 1025–1026, 1049, 1118, 1140–1141, 1156–1157, 1175–1176, 1307–1308, 1324, 1326, 1405–1413, 1508, 1563, 1578, 1614–1615, 1617–1619, 1633–1635, 1645–1646, 1652, 1667
TOTAL20473325084% 

Python Unit Test Overview

Tests Skipped Failures Errors Time
3922 225 💤 0 ❌ 0 🔥 1m 7s ⏱️

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

This PR fixes a token waste issue where instructions were being repeatedly sent to the OpenAI Responses API on every turn of a conversation, even though the API maintains server-side state and only needs instructions on the first turn.

Changes:

  • Modified _prepare_options in OpenAIResponsesClient to conditionally prepend instructions only when no conversation_id exists (first turn)
  • Added comprehensive regression tests to verify instructions are sent on first turn but skipped on continuations
  • Fix applies to both OpenAI and Azure Responses API clients through inheritance

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
python/packages/core/agent_framework/openai/_responses_client.py Added logic to skip instruction prepending when conversation_id (resp_* or conv_*) is present, preventing repeated system messages
python/packages/core/tests/openai/test_responses_instructions_continuation.py New test file with 4 comprehensive tests covering first turn, continuation with resp_ ID, continuation with conv_ ID, and baseline without conversation_id

Comment thread python/packages/core/tests/openai/test_responses_instructions_continuation.py Outdated
Merged via the queue into microsoft:main with commit e9b3a5b Feb 13, 2026
25 checks passed
@eavanvalkenburg
Eduard van Valkenburg (eavanvalkenburg) deleted the fix-3498-no-repeat-instructions branch June 30, 2026 09:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

python Usage: [Issues, PRs], Target: Python

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Python: [Feature]: Don't send repeated system messages with OpenAIResponses

5 participants