Python: [Feature]: Support OpenAI instructions in Responses API - #7292
Conversation
There was a problem hiding this comment.
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 prependedsystemmessage; allows it to flow through as the top-level Responses APIinstructionsfield. - Updates
OpenAIChatOptionsto include aninstructions: strentry for type safety/autocomplete. - Updates unit tests to assert
instructionsis passed top-level and thatinputis 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
left a comment
There was a problem hiding this comment.
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
Python Test Coverage Report •
Python Unit Test Overview
|
||||||||||||||||||||||||||||||
|
Hey @PratikWayase thanks for the contribution! Can you please address the failing typing checks |
gnanirahulnutakki
left a comment
There was a problem hiding this comment.
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.
eavanvalkenburg
left a comment
There was a problem hiding this comment.
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
left a comment
There was a problem hiding this comment.
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.
Motivation & Context
The OpenAI Responses API supports an
instructionsparameter 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 asystemmessage 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 OpenAIinstructionsparameter as intended for temporary, per-turn overrides.Description & Review Guide
instructionsfrom theexclude_keysset in_prepare_optionsin_chat_client.py, allowing it to flow natively into the OpenAI API payload.instructionsas asystemmessage to themessagesarray.prepend_instructions_to_messagesimport.instructions: strfield to theOpenAIChatOptionsTypedDict for type safety and IDE autocompletion.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 updatedtest_get_response_with_all_parametersto reflect the new behavior.instructionspassed viaoptionswill now only apply to the current response. Continuation turns usingconversation_idorprevious_response_idwill no longer inherit the temporary instructions._prepare_optionscorrectly passes theinstructionsparameter to the underlying OpenAI SDK without side effects.Related Issue
Fixes #7056
Contribution Checklist
breaking changelabel (or add "[BREAKING]" to the title prefix, before or after any language prefix) — a workflow keeps the label and title prefix in sync automatically.