Skip to content

feat(realtime): add input guardrails for RealtimeAgent and RealtimeRunConfig#3721

Open
Skyline-9 wants to merge 11 commits into
openai:mainfrom
Skyline-9:feat/realtime-input-guardrails
Open

feat(realtime): add input guardrails for RealtimeAgent and RealtimeRunConfig#3721
Skyline-9 wants to merge 11 commits into
openai:mainfrom
Skyline-9:feat/realtime-input-guardrails

Conversation

@Skyline-9

@Skyline-9 Skyline-9 commented Jul 1, 2026

Copy link
Copy Markdown

Summary

Adds input guardrails to the realtime API, bringing it closer to parity with the non-realtime Agent/Runner, which already supports input_guardrails. Realtime today only supports output guardrails (RealtimeAgent.output_guardrails / RealtimeRunConfig["output_guardrails"]); there is no first-class way to screen the user's transcribed input.

What changed:

  • RealtimeAgent.input_guardrails (appended at the end of the dataclass, default_factory=list) and RealtimeRunConfig["input_guardrails"] (NotRequired TypedDict key).
  • New RealtimeInputGuardrailTripped session event (appended at the end of the RealtimeSessionEvent union), mirroring RealtimeGuardrailTripped field-for-field but typed to InputGuardrailResult.
  • RealtimeSession runs the combined agent + run-config input guardrails on the completed user transcript (input_audio_transcription_completed), de-duped by id(). It reuses the existing output-guardrail machinery (shared _guardrail_tasks set, _on_guardrail_task_done, _cleanup_guardrail_tasks), so close() cancels in-flight tasks. On a trip it emits input_guardrail_tripped, forces response.cancel, and sends a follow-up user message naming the guardrail.
  • Exported from agents.realtime.__init__ (__all__) with an import regression test.
  • Docs: docs/ref/realtime/events.md renders the new event; docs/realtime/guide.md documents the feature and disambiguates it from the existing tool-level "input guardrails on function-tool calls".

The design deliberately mirrors _run_output_guardrails (argument order verified against InputGuardrail.run(self, agent, input, context)) so the behavior and lifecycle are consistent with what maintainers already review.

Known limitation (documented, not hidden)

The forced cancel reliably interrupts a response that is already in flight. If a guardrail resolves in the narrow window before any response has been created for the tripped turn, the cancel is a no-op and that response may proceed. Eliminating this window cleanly requires response<->user-item correlation at the model layer (for example a response_id on turn-started / response-created) so the session can cancel only the tripped turn's response without also cancelling the intentional guardrail-notification response. This limitation is documented in the RealtimeInputGuardrailTripped docstring, RealtimeAgent.input_guardrails, and the guide rather than papered over with a heuristic that would cancel the wrong response. Scope is also documented: input guardrails run on transcribed audio only; text sent via send_message is not screened. Happy to pursue the model-layer correlation as a follow-up if maintainers prefer.

Test plan

  • Added tests/realtime/test_session.py::TestInputGuardrailFunctionality, including edge cases:
    • a raising guardrail is skipped and does not crash the shared guardrail task,
    • raising + tripping guardrails together still produce exactly one interrupt with the tripping guardrail named,
    • a second transcription for an already-tripped item is de-duplicated,
    • no guardrail task is created when none are configured.
  • Ran the standard verification stack from the repo root:
    • make format, make lint, make typecheck — pass
    • make tests (full) — pass (4797 passed, 2 skipped; serial 27 passed, 5 skipped)
    • make build-docs — pass (new RealtimeInputGuardrailTripped reference resolves clean)

Issue number

Realtime parity with the non-realtime input-guardrail support. Happy to link the relevant tracking issue.

Checks

  • I've added new tests, if relevant
  • I've run .agents/skills/code-change-verification/scripts/run.sh
  • I've confirmed all verification steps pass (ran make format, make lint, make typecheck, make tests, and make build-docs)
  • If using Codex, I've run /review before submitting this PR

Compatibility notes

Additive. New fields are appended at the end of RealtimeAgent (preserving positional compatibility) and are a NotRequired config key; the new event is appended at the end of the RealtimeSessionEvent union. Sessions with no input guardrails configured create no extra tasks per utterance.

@chatgpt-codex-connector chatgpt-codex-connector Bot 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 1b2c6fc6b1

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread src/agents/realtime/session.py Outdated
Comment thread src/agents/realtime/session.py Outdated
@Skyline-9

Copy link
Copy Markdown
Author

@codex review

@Skyline-9 Skyline-9 force-pushed the feat/realtime-input-guardrails branch from 1b2c6fc to bedd7a9 Compare July 1, 2026 23:32

@chatgpt-codex-connector chatgpt-codex-connector Bot 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: bedd7a9c37

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread src/agents/realtime/session.py Outdated
@Skyline-9

Copy link
Copy Markdown
Author

@codex review

@Skyline-9 Skyline-9 force-pushed the feat/realtime-input-guardrails branch from bedd7a9 to 7e2676a Compare July 2, 2026 18:52

@chatgpt-codex-connector chatgpt-codex-connector Bot 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 7e2676af1d

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread src/agents/realtime/session.py Outdated

@chatgpt-codex-connector chatgpt-codex-connector Bot 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 7e2676af1d

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread src/agents/realtime/session.py Outdated
@Skyline-9 Skyline-9 force-pushed the feat/realtime-input-guardrails branch from 7e2676a to 058565e Compare July 3, 2026 07:39

@chatgpt-codex-connector chatgpt-codex-connector Bot 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 058565e6fd

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread src/agents/realtime/session.py

@chatgpt-codex-connector chatgpt-codex-connector Bot 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: eb71f7ba7f

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread docs/superpowers/plans/2026-07-08-realtime-input-guardrail-interrupt.md Outdated
@Skyline-9 Skyline-9 force-pushed the feat/realtime-input-guardrails branch from eb71f7b to fa7fcdf Compare July 11, 2026 20:28

@chatgpt-codex-connector chatgpt-codex-connector Bot 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: fa7fcdf92e

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread src/agents/realtime/session.py

@chatgpt-codex-connector chatgpt-codex-connector Bot 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: db7063f776

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread src/agents/realtime/session.py
Skyline-9 added 10 commits July 12, 2026 02:00
Snapshot the active agent and its input guardrails when the transcription event is handled so a concurrent update_agent()/handoff cannot run a different agent's guardrails, and run the input guardrails concurrently so a slow guardrail cannot delay the forced response cancel.
Address Codex review: replace asyncio.gather (which waits for every
guardrail before inspecting results) with asyncio.as_completed so the
forced response cancel fires as soon as the first tripwire is available,
then cancel the remaining guardrail tasks. Mirrors the streamed
input-guardrail path so a slow guardrail can no longer delay the
interrupt.
@Skyline-9 Skyline-9 force-pushed the feat/realtime-input-guardrails branch from db1236c to 03bc59a Compare July 12, 2026 09:01
@Skyline-9

Copy link
Copy Markdown
Author

Hi team,

I have updated the PR to address the recent Codex review comments, rebased it on top of the latest main, and verified that all tests/checks pass successfully.

Key Updates:

  1. Preventing Guardrail Bypass: When an input guardrail trips, the session now sends a conversation.item.delete client event to remove the offending transcribed user input item from the conversation history. This ensures that the replacement response generated in response to the guardrail notification does not see or act on the disallowed input.
  2. Rejection of Sequential/Blocking Guardrails: Because the Realtime API automatically starts streaming a response concurrently upon user silence (turn-detection), sequential blocking guardrails (run_in_parallel=False) cannot prevent response generation and are incompatible. The session now raises a ValueError if any input guardrail is configured with run_in_parallel=False (fully documented in docs/realtime/guide.md).
  3. Rebase & Verification: Rebased cleanly on top of upstream/main. Run make format, make lint, make typecheck, and all tests pass cleanly.

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 03bc59aa42

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread src/agents/realtime/session.py Outdated
@Skyline-9

Copy link
Copy Markdown
Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Delightful!

Reviewed commit: a4edd34caf

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants