Repository navigation
feat(server): add OpenAI Chat buffered projections #2413
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Pouyanpi
merged 34 commits into
develop
from
pouyanpi/openai-chat-buffered-projections-3
Oct 9, 2026
+2,953
−5
Merged
Changes from all commits
Commits
Show all changes
34 commits
Select commit
Hold shift + click to select a range
7ed8e8b
feat(server): pin the OpenAI Chat provider source
Pouyanpi e7c05c2
feat(server): stage OpenAI Chat buffered projections
Pouyanpi bdb3149
fix(server): harden guarded payload parsing
Pouyanpi 425cafc
test(server): make JSON integer regression deterministic
Pouyanpi 6725877
refactor(server): declare Chat buffered policy in typed models
Pouyanpi 3072daa
fix(server): export nullable policy using disjoint schema branches
Pouyanpi 2f4b6c7
refactor(server): clarify and document projection policy declarations
Pouyanpi 72e5e28
docs(server): carry contract cleanup into buffered projections
Pouyanpi 850c36e
fix(server): reject non-integer n in Chat requests
Pouyanpi ab757d3
fix(server): reject member names that differ only by case
Pouyanpi 79700a0
fix(server): close the buffered Chat response choice and message
Pouyanpi 94be89b
fix(server): accept an empty tool_calls list in Chat responses
Pouyanpi 76aaa57
fix(server): reject Chat requests for log probabilities
Pouyanpi 82909cd
fix(server): reject policy constraints the contract cannot express
Pouyanpi 197651d
refactor(server): keep the OpenAI provider pin in one place
Pouyanpi 1455bcc
fix(server): close the Chat request to OpenAI's fields
Pouyanpi 265c887
fix(server): close the buffered Chat response to OpenAI's fields
Pouyanpi 436f63f
fix(server): reject citation annotations in Chat responses
Pouyanpi 47177c9
fix(server): reject participant names on guarded Chat messages
Pouyanpi 43b5f7d
fix(server): accept only OpenAI reasoning efforts in Chat requests
Pouyanpi 352bb02
fix(server): limit parser duplicate checks to exact member names
Pouyanpi d1594d6
docs(server): list every content-free exception to null-only fields
Pouyanpi 754abb6
fix(server): reject replacement blockers that name no field
Pouyanpi b832b1f
test(server): validate Chat exports against the guard contract schema
Pouyanpi c4b71bc
fix(server): check policy formats when declarations are made
Pouyanpi 1d0a3aa
refactor(server): enforce unknown-field policy in the policy layer
Pouyanpi db97258
fix(server): treat empty Chat responses as having nothing to inspect
Pouyanpi ba048e4
fix(server): export reviewed opaque names as policy properties
Pouyanpi cbdc083
test(server): compare export-derived acceptance with the runtime
Pouyanpi d4a4fe7
docs(server): state that the empty-response relay is not yet wired
Pouyanpi 78ef9f0
test(server): match export reference to Unicode case folding
Pouyanpi b9dbc7d
test(server): cover projection policy and payload error paths
Pouyanpi 0ea47d4
test(server): separate generic policy and OpenAI projection tests
Pouyanpi 7504628
style: tighten docstrings
Pouyanpi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| # SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| import json | ||
| import math | ||
| from collections.abc import Mapping, Sequence | ||
| from typing import Any | ||
|
|
||
| from nemoguardrails.server.experimental.provider.types import JsonObject | ||
|
|
||
|
|
||
| class InvalidJson(ValueError): | ||
| """Report a payload that is not standards-compliant JSON.""" | ||
|
|
||
|
|
||
| class UnsupportedJsonShape(ValueError): | ||
| """Report valid JSON that cannot be handled without ambiguity.""" | ||
|
|
||
|
|
||
| def _unique_object(pairs: Sequence[tuple[str, Any]]) -> JsonObject: | ||
| """Build an object while rejecting duplicate member names. | ||
|
|
||
| Names that differ only by case are left to the projection models, which | ||
| close every reviewed object. Opaque provider data may legitimately use | ||
| such names. | ||
| """ | ||
| result: JsonObject = {} | ||
| for key, value in pairs: | ||
| if key in result: | ||
| raise UnsupportedJsonShape(f"Duplicate JSON member {key!r} is not supported.") | ||
| result[key] = value | ||
| return result | ||
|
|
||
|
|
||
| def _reject_nonstandard_number(value: str): | ||
| """Reject JSON constants that are not part of the standard grammar.""" | ||
| raise InvalidJson(f"Non-standard JSON number {value!r} is not supported.") | ||
|
|
||
|
|
||
| def _parse_finite_float(value: str) -> float: | ||
| """Parse a JSON float only when it has a finite value.""" | ||
| parsed = float(value) | ||
| if not math.isfinite(parsed): | ||
| raise InvalidJson(f"Non-finite JSON number {value!r} is not supported.") | ||
| return parsed | ||
|
|
||
|
|
||
| def _parse_integer(value: str) -> int: | ||
| """Parse an integer while normalizing interpreter size-limit failures.""" | ||
| try: | ||
| return int(value) | ||
| except ValueError as error: | ||
| raise InvalidJson("The JSON integer exceeds the supported size.") from error | ||
|
|
||
|
|
||
| def parse_json_object(body: bytes) -> JsonObject: | ||
| """Parse a JSON object without accepting ambiguous or nonstandard input.""" | ||
|
|
||
| if not isinstance(body, bytes): | ||
| raise TypeError("A provider JSON body must be bytes.") | ||
| try: | ||
| payload = json.loads( | ||
| body, | ||
| object_pairs_hook=_unique_object, | ||
| parse_constant=_reject_nonstandard_number, | ||
| parse_float=_parse_finite_float, | ||
| parse_int=_parse_integer, | ||
| ) | ||
| except UnsupportedJsonShape: | ||
| raise | ||
| except (UnicodeDecodeError, json.JSONDecodeError, RecursionError, InvalidJson) as error: | ||
| raise InvalidJson("The payload must be valid JSON.") from error | ||
| if not isinstance(payload, dict): | ||
|
Pouyanpi marked this conversation as resolved.
|
||
| raise UnsupportedJsonShape("The JSON payload must be an object.") | ||
| return payload | ||
|
|
||
|
|
||
| def encode_json_object(payload: Mapping[str, Any]) -> bytes: | ||
| """Serialize a modified provider object as compact UTF-8 JSON.""" | ||
|
|
||
| return json.dumps(payload, ensure_ascii=False, separators=(",", ":"), allow_nan=False).encode() | ||
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
54 changes: 54 additions & 0 deletions
54
nemoguardrails/server/experimental/contracts/openai/README.md
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| # OpenAI Chat Completions contract | ||
|
|
||
| Operation `createChatCompletion` uses the `single_text.v1` capability profile | ||
| for its guarded text boundary. This page summarizes that boundary and its | ||
| provider source. Neither the summary nor the source pin enables runtime | ||
| behavior. See [Guard contracts](../README.md) for the document vocabulary. | ||
|
|
||
| ## Guarded boundary | ||
|
|
||
| | Area | Single-text boundary | | ||
| | --- | --- | | ||
| | Request | One user message with non-empty string content at `messages[0].content`. | | ||
| | Buffered response | One assistant choice with string content at `choices[0].message.content`. Empty or null content means the response has nothing to inspect, because every other reviewed field is content-free. The projection reports this through `has_text`; relaying such a response without output checks is left to the integration. | | ||
| | Constrained values | User/assistant roles, single-item arrays, and request `n` constrained to one. | | ||
| | Unsupported content | Tool, audio, multimodal, refusal, participant name, and separate reasoning content where explicitly disabled. | | ||
| | Opaque data | Reviewed provider-owned metadata and controls, not additional guarded subjects. | | ||
| | Closed objects | The request, its user message, the buffered response, and its choice and assistant message reject members outside OpenAI's fields. The user message, choice, and assistant message are configurable: only trusted configuration may allow unknown members there. Fields specific to compatible servers belong to their own reviewed extensions. | | ||
|
|
||
| This request boundary does not accept conversation histories, system/developer | ||
| messages alongside the user message, or multimodal content blocks. Disabled | ||
| fields are null-only: a non-null value does not become acceptable merely because | ||
| it requests plain text or no tools. A few unsupported features are constrained | ||
| instead, because OpenAI's schema also allows a value that carries no content: | ||
| response `tool_calls` and `annotations` accept null or an empty list, and | ||
| request `logprobs` accepts null or `false`. | ||
|
|
||
| Log probabilities are unsupported: they carry token text that rails do not | ||
| inspect. Request `logprobs` accepts only `false` or null, request `top_logprobs` | ||
| accepts only null, and buffered response `choices[0].logprobs` accepts only null. | ||
| The request check applies even when output inspection is off. | ||
|
|
||
| Text replacement eligibility is separate from endpoint support for replacement | ||
| outcomes. Response annotations must be null or empty, because citation text is | ||
| not inspected. The policy still declares that non-empty annotations would block | ||
| text replacement. Unrelated provider data must remain intact. | ||
|
|
||
| The Python projections, bindings, and endpoint determine exact acceptance and | ||
| runtime behavior. Their machine-readable contract belongs with the integration, | ||
| not in a separately maintained handwritten policy here. Recognizing the request | ||
| `stream` flag does not itself provide a streaming endpoint. Streaming needs its | ||
| own event classification, lifecycle handling, and implementation tests; the | ||
| buffered boundary is not a claim about accepted stream events. | ||
|
|
||
| ## Provider provenance | ||
|
|
||
| [source.yaml](source.yaml) records the OpenAPI document's immutable revision, | ||
| document version, download location, and expected SHA-256 digest. The pin is a | ||
| review baseline, not a claim to cover every current OpenAI field or compatible | ||
| provider. | ||
|
|
||
| The offline metadata tests check pin structure and URL/revision consistency. | ||
| They do not fetch the document or verify its bytes against the digest. Reviewing | ||
| a provider update requires checking the source artifact and changes to guarded, | ||
| constrained, disabled, and opaque fields separately from format validation. |
5 changes: 5 additions & 0 deletions
5
nemoguardrails/server/experimental/contracts/openai/source.yaml
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| document_url: https://github.com/openai/openai-openapi/blob/df63773f69f542ef875b9f00c3837c25ba5f4f2a/openapi.yaml | ||
|
Pouyanpi marked this conversation as resolved.
|
||
| download_url: https://raw.githubusercontent.com/openai/openai-openapi/df63773f69f542ef875b9f00c3837c25ba5f4f2a/openapi.yaml | ||
| revision: df63773f69f542ef875b9f00c3837c25ba5f4f2a | ||
| document_version: 2.3.0 | ||
| sha256: f2dae1a9aced09b91310db89edda51bf1e36ecbfb05230c3a50b239c07708469 | ||
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.