Skip to content

Python: [Feature]: Support OpenAI instructions in Responses API - #7292

Merged
eavanvalkenburg merged 3 commits into
microsoft:mainfrom
PratikWayase:feature/support-openai-instructions-responses-api
Jul 27, 2026
Merged

Python: [Feature]: Support OpenAI instructions in Responses API#7292
eavanvalkenburg merged 3 commits into
microsoft:mainfrom
PratikWayase:feature/support-openai-instructions-responses-api

Conversation

@PratikWayase

Copy link
Copy Markdown
Contributor

Motivation & Context

The OpenAI Responses API supports an instructions parameter that applies instructions to a single request ephemerally, without persisting in the conversation state. Previously, the agent framework intercepted this parameter and injected it as a system message into the chat history. This caused the instructions to persist across all future turns in the session, defeating the purpose of the ephemeral parameter and making it impossible to do per-request steering (like temporarily asking the agent to refuse a request). This PR enables developers to use the native OpenAI instructions parameter as intended for temporary, per-turn overrides.

Description & Review Guide

  • What are the major changes?
    • Removed instructions from the exclude_keys set in _prepare_options in _chat_client.py, allowing it to flow natively into the OpenAI API payload.
    • Removed the logic that prepended instructions as a system message to the messages array.
    • Removed the now-unused prepend_instructions_to_messages import.
    • Added the instructions: str field to the OpenAIChatOptions TypedDict for type safety and IDE autocompletion.
    • Updated test_openai_chat_client.py: removed outdated tests that expected system message injection, added a new parameterized test (test_instructions_passed_natively_not_as_system_message) to verify instructions are passed top-level and don't mutate the input messages, and updated test_get_response_with_all_parameters to reflect the new behavior.
  • What is the impact of these changes?
    • instructions passed via options will now only apply to the current response. Continuation turns using conversation_id or previous_response_id will no longer inherit the temporary instructions.
  • What do you want reviewers to focus on?
    • Verify that removing the manual prepend logic in _prepare_options correctly passes the instructions parameter to the underlying OpenAI SDK without side effects.
    • Ensure the test coverage adequately proves the ephemeral nature of the instructions across continuation turns.

Related Issue

Fixes #7056

Contribution Checklist

  • The code builds clean without any errors or warnings
  • All unit tests pass, and I have added new tests where possible
  • The PR follows the Contribution Guidelines
  • This PR is linked to an issue and there is no other open PR for this issue (see Related Issue above).
  • This is not a breaking change. If it is a breaking change, add the breaking change label (or add "[BREAKING]" to the title prefix, before or after any language prefix) — a workflow keeps the label and title prefix in sync automatically.

Copilot AI review requested due to automatic review settings July 23, 2026 14:23
@agent-framework-automation agent-framework-automation Bot added the python Usage: [Issues, PRs], Target: Python label Jul 23, 2026

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

Enables native use of the OpenAI Responses API instructions parameter in the Python OpenAI chat client so per-request steering remains ephemeral (i.e., it no longer persists by being injected into conversation message history).

Changes:

  • Stops converting options["instructions"] into a prepended system message; allows it to flow through as the top-level Responses API instructions field.
  • Updates OpenAIChatOptions to include an instructions: str entry for type safety/autocomplete.
  • Updates unit tests to assert instructions is passed top-level and that input is not mutated with an injected system message.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
python/packages/openai/agent_framework_openai/_chat_client.py Removes system-message injection behavior and passes instructions natively to the Responses API.
python/packages/openai/tests/openai/test_openai_chat_client.py Updates/rewrites tests to validate native instructions passthrough and absence of injected system messages.

@giles17 giles17 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.

Automated Code Review

Reviewers: 5 | Confidence: 93% | Result: All clear

Reviewed: Correctness, Security Reliability, Test Coverage, Failure Modes, Design Approach


Automated review by giles17's agents

@github-actions

github-actions Bot commented Jul 23, 2026

Copy link
Copy Markdown
Contributor

Python Test Coverage

Python Test Coverage Report •
FileStmtsMissCoverMissing
packages/openai/agent_framework_openai
   _chat_client.py136611791%312, 325, 675–679, 687–690, 696–700, 750–757, 759–761, 768–770, 828, 836, 859, 1076, 1135, 1137, 1139, 1141, 1207, 1221, 1301, 1311, 1316, 1359, 1471–1472, 1487, 1752, 1859, 1864–1865, 1948, 1958, 1985, 1991, 2001, 2007, 2012, 2018, 2023–2024, 2104, 2148, 2151–2154, 2168, 2178–2179, 2191, 2233, 2298, 2315, 2318, 2345–2347, 2386, 2403, 2406, 2468, 2475, 2512–2513, 2548, 2586–2587, 2605–2606, 2778–2779, 2797, 2883–2891, 3069, 3084, 3134, 3144–3146, 3173–3175, 3185–3186, 3192, 3207, 3340–3341
TOTAL45562447490% 

Python Unit Test Overview

Tests Skipped Failures Errors Time
9406 33 💤 0 ❌ 0 🔥 2m 34s ⏱️

@giles17

giles17 commented Jul 23, 2026

Copy link
Copy Markdown
Contributor

Hey @PratikWayase thanks for the contribution! Can you please address the failing typing checks

@giles17 giles17 self-assigned this Jul 23, 2026

@gnanirahulnutakki gnanirahulnutakki left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

I verified the native Responses API passthrough against OpenAI Python SDK 2.45.0 and the current official API contract, then reproduced the PR's typing gate locally. The runtime behavior is sound; the inline comment identifies the isolated test-typing failure and a validated minimal fix.

Comment thread python/packages/openai/tests/openai/test_openai_chat_client.py Outdated

@eavanvalkenburg eavanvalkenburg left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

do we need to do the same for other providers?

Comment thread python/packages/openai/agent_framework_openai/_chat_client.py Outdated
Comment thread python/packages/openai/agent_framework_openai/_chat_client.py Outdated
Comment thread python/packages/openai/agent_framework_openai/_chat_client.py Outdated
@PratikWayase

Copy link
Copy Markdown
Contributor Author

do we need to do the same for other providers?

I kept this PR scoped strictly to the OpenAI Responses API fix since that's where the native instructions parameter is specifically supported. I can open a separate follow-up issue/PR to audit the other providers (Anthropic, Gemini) to ensure they don't have redundant fields or suboptimal instruction handling. Let me know if you'd prefer I tackle that here or in a follow-up

@gnanirahulnutakki gnanirahulnutakki left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Re-reviewed exact head 8a6a52c9b1b2 after the rebase. The original OpenAIChatOptions typing defect is resolved. I reran the four focused instructions/all-parameters runtime cases, all five package typing checkers (mypy, pyright, pyrefly, ty, and zuban), and strict package Pyright; everything passes.

@eavanvalkenburg
eavanvalkenburg added this pull request to the merge queue Jul 27, 2026
Merged via the queue into microsoft:main with commit 754cbe5 Jul 27, 2026
37 checks passed
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]: Support OpenAI instructions in Responses API

6 participants