Python: fix: prevent repeating instructions in continued Responses API conversations#3909
Merged
eavanvalkenburg merged 3 commits intomicrosoft:mainfrom Feb 13, 2026
Conversation
…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
Member
Python Test Coverage Report •
Python Unit Test Overview
|
||||||||||||||||||||||||||||||
Contributor
There was a problem hiding this comment.
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_optionsinOpenAIResponsesClientto 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 |
python/packages/core/tests/openai/test_responses_instructions_continuation.py
Outdated
Show resolved
Hide resolved
SergeyMenshykh
approved these changes
Feb 13, 2026
westey-m
approved these changes
Feb 13, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #3498
This PR prevents instructions from being repeatedly sent to the OpenAI Responses API when continuing a conversation.
Changes
_prepare_options()inpackages/core/agent_framework/openai/_responses_client.pyto prepend instructions only on the first turn (when noconversation_id/response_idexists).Testing
packages/core/tests/openai/test_openai_responses_client.pycovering:resp_format ID skips instructions.conv_format ID skips instructions.uv run ruff format packages/core/tests/openai/test_openai_responses_client.pyuv run ruff check packages/core/tests/openai/test_openai_responses_client.pyuv 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.