Python: Add OpenAI Codex SDK integration (agent-framework-codex)#4375
Python: Add OpenAI Codex SDK integration (agent-framework-codex)#4375LEDazzio01 wants to merge 12 commits intomicrosoft:mainfrom
Conversation
There was a problem hiding this comment.
Pull request overview
Adds a new Python managed-agent integration package, agent-framework-codex, that wraps the Codex SDK to provide an Agent Framework BaseAgent implementation with streaming, sessions, tool bridging (via MCP), and structured-output propagation.
Changes:
- Introduces
CodexAgent,CodexAgentOptions, andCodexAgentSettingsplus supporting implementation to run Codex viaCodexSDKClientwith streaming updates and session resume support. - Adds a full unit-test suite for initialization, streaming/non-streaming runs, sessions, tool conversion, runtime options, and structured output.
- Adds package scaffolding (
pyproject.toml, README, LICENSE, AGENTS.md).
Reviewed changes
Copilot reviewed 7 out of 7 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| python/packages/codex/agent_framework_codex/_agent.py | Implements the Codex managed agent (client lifecycle, sessions, tool→MCP conversion, streaming). |
| python/packages/codex/tests/test_codex_agent.py | Adds mocked unit tests covering key agent behaviors. |
| python/packages/codex/pyproject.toml | Defines the new package metadata, dependencies, and tooling config. |
| python/packages/codex/agent_framework_codex/init.py | Exposes the package public API + version. |
| python/packages/codex/README.md | Minimal install/get-started documentation. |
| python/packages/codex/LICENSE | Adds MIT license text for the package. |
| python/packages/codex/AGENTS.md | Agent discovery metadata for the new package. |
LEDazzio01
left a comment
There was a problem hiding this comment.
Thanks for the thorough review! All 3 comments addressed in the latest commits:
-
_ensure_sessionsession isolation (_agent.py) — Fixed. Removed the extrasession_id andguard soNoneis now treated as a distinct session identity. Added a dedicated regression test (test_ensure_session_recreates_when_resumed_then_fresh) that verifies switching from a resumed session back toNonecorrectly creates a new client. (commit8df3a83,3b95a7d) -
pyproject.tomldependency — Alignedagent-framework-core>=1.0.0rc2→>=1.0.0rc1to match the other managed-agent integrations. (commit850cb45) -
AGENTS.mddescription — Updated "Pydantic settings" → "TypedDict-based settings populated via the framework'sload_settings()helper". (commitebdcb5e)
Adds LICENSE, README.md, AGENTS.md, and pyproject.toml for the new agent-framework-codex package. Closes microsoft#4370.
Core implementation of the agent-framework-codex package with: - CodexAgentSettings for env-based configuration - CodexAgentOptions for per-request options - CodexAgent wrapping CodexSDKClient with streaming, tools, sessions
Tests cover settings, initialization, lifecycle, run (streaming and non-streaming), session management, tool conversion, permissions, error handling, format prompt, options preparation, and structured output.
Co-authored-by: Copilot <copilot@github.com>
Co-authored-by: Copilot <copilot@github.com>
Previously, _ensure_session() only recreated the client when (session_id and session_id != self._current_session_id), which meant switching from a resumed session back to a fresh session (None) would incorrectly reuse the existing client. Now we compare session_id != self._current_session_id directly, so None is treated as a valid, distinct session identity. Co-authored-by: Copilot <copilot@github.com>
Verifies that _ensure_session creates a new client when switching from a resumed session (non-None) back to a fresh session (None). Co-authored-by: Copilot <copilot@github.com>
- Rebase onto latest main - Bump agent-framework-core dependency to >=1.0.0rc2 - Split CodexAgent into RawCodexAgent + CodexAgent(AgentTelemetryLayer) following A2AAgent pattern as requested - Remove MutableMapping from run() options parameter type - Export RawCodexAgent from __init__.py
3b95a7d to
b752405
Compare
|
Thanks for the thorough review @eavanvalkenburg! I've addressed all feedback in the latest force-push (rebased onto latest main): ✅ Changes made:
Ready for another look! 🙏 |
eavanvalkenburg
left a comment
There was a problem hiding this comment.
looks pretty good, just missing a sample to showcase the user code, should be still inside this folder, that will be moved once we include this package structurally
- Simplify typing: remove MutableMapping union, use just OptionsT - Update dependency to agent-framework-core>=1.0.0rc2 - Change AGENT_PROVIDER_NAME from "openai.codex" to "openai" per OTel spec - Add sample script showcasing basic CodexAgent usage
LEDazzio01
left a comment
There was a problem hiding this comment.
Thanks for the thorough review @eavanvalkenburg! All feedback has been addressed in commit 4b93a97:
-
Rebase + rc2 — Updated
pyproject.tomldependency toagent-framework-core>=1.0.0rc2. -
Telemetry split — Already done in a prior commit:
RawCodexAgentholds the implementation,CodexAgent(AgentTelemetryLayer, RawCodexAgent)adds telemetry. Both are exported from__init__.py. -
Provider name — Changed
AGENT_PROVIDER_NAMEfrom"openai.codex"to"openai"to follow the OTel GenAI semantic conventions. The model name (codex-mini-latest/gpt-5.1-codex) already differentiates Codex telemetry from standard OpenAI chat. -
Simplified typing — Removed the
MutableMapping[str, Any]union fromdefault_optionsandoptionsparameters across__init__and allrun()overloads. Now uses justOptionsTas suggested. Also removed the unusedMutableMappingimport. -
Background support — The Codex SDK currently runs synchronously until completion (with streaming). Background mode would require the SDK to support detaching from a running session and polling for results, which isn't available yet. I'd recommend deferring this to a follow-up PR once the SDK adds that capability. Happy to track it as an issue if you'd like.
-
Sample script — Added
samples/codex_sample.pyshowcasing basic usage, streaming, multi-turn sessions, and custom tool integration via the@tooldecorator.
|
@LEDazzio01 thanks for your patience with this, we have a bunch of new guidance for package management in the skill for that, could you ensure we apply that with this as a |
…odex Per maintainer guidance and python-package-management SKILL.md: - Update version from 1.0.0b260225 to 1.0.0a260410 (alpha pattern) - Update classifier from Beta to Alpha (Development Status :: 3 - Alpha) - Update agent-framework-core dependency from >=1.0.0rc2 to >=1.0.1,<2 - Add upper bound to codex-sdk: >=0.1.0,<0.2 - Add pyright include directive for agent_framework_codex - Use structured poe task format with help + cmd keys - Exclude integration tests from default test run - Register package in root pyproject.toml tool.uv.sources - Add codex to AGENTS.md package documentation index
|
@eavanvalkenburg Thanks for the pointer to the package management skill! I've applied the full alpha package checklist from Changes in commit
Note: The Ready for review! 🚀 |
Per python-package-management SKILL.md alpha checklist item microsoft#8.
Motivation and Context
Closes #4370
The Microsoft Agent Framework currently supports managed agent SDK integrations for Claude (
agent-framework-claude) and GitHub Copilot (agent-framework-github-copilot), but not for OpenAI Codex. This PR addsagent-framework-codexto provide parity across the three major agentic coding SDKs.Description
Adds a new Python package
agent-framework-codexthat wraps thecodex-sdkas a managed agent, following the same conventions as the existing Claude and GitHub Copilot packages.New Files
python/packages/codex/pyproject.tomlpython/packages/codex/README.mdpython/packages/codex/LICENSEpython/packages/codex/AGENTS.mdpython/packages/codex/agent_framework_codex/__init__.pyCodexAgent,CodexAgentOptions,CodexAgentSettingspython/packages/codex/agent_framework_codex/_agent.pyCodexSDKClientpython/packages/codex/tests/test_codex_agent.pyKey Classes
CodexAgentSettings— TypedDict for env-based config (CODEX_AGENT_prefix)CodexAgentOptions— TypedDict for per-request options (model, system_prompt, etc.)CodexAgent—BaseAgentsubclass with full support for:service_session_id)async with CodexAgent() as agent)Usage
Contribution Checklist