Skip to content

fix(server): keep OpenAIChatMessage validator importable across the supported pydantic range - #2324

Closed
feiiiiii5 wants to merge 1 commit into
NVIDIA-NeMo:developfrom
feiiiiii5:fix/openai-chat-message-pydantic-compat
Closed

feiiiiii5 wants to merge 1 commit into
NVIDIA-NeMo:developfrom
feiiiiii5:fix/openai-chat-message-pydantic-compat

Conversation

@feiiiiii5

Copy link
Copy Markdown

Summary

Fixes #2292.

OpenAIChatMessage passed json_schema_input_type to BeforeValidator, but that argument is only accepted from pydantic 2.9 onwards, while the project declares pydantic>=2.5,<3.0. Importing nemoguardrails.server.schemas.openai on pydantic 2.5–2.8 therefore raised:

TypeError: BeforeValidator.__init__() got an unexpected keyword argument 'json_schema_input_type'

This change builds the validator through a small helper that applies the argument when the installed pydantic supports it and falls back to a plain BeforeValidator otherwise:

def _build_chat_message_validator() -> BeforeValidator:
    try:
        return BeforeValidator(
            _validate_openai_chat_message,
            json_schema_input_type=_OpenAIChatMessageInput,
        )
    except TypeError:
        return BeforeValidator(_validate_openai_chat_message)

On pydantic < 2.9 the generated JSON schema describes messages as generic objects instead of the per-role union; runtime validation behavior (string role required, per-role payload validation) is identical.

I kept the declared dependency range as-is rather than bumping the minimum to 2.9, per the issue's "either remove/replace the argument or raise the minimum" — happy to switch to a floor bump + lockfile regeneration if maintainers prefer that route.

Verification

  • Reproduced the original TypeError empirically on pydantic 2.5.1, 2.6.4, 2.7.4 and 2.8.2 before the change; confirmed 2.9.2 accepts the argument.
  • New tests in tests/server/test_openai_schemas_compat.py:
    • valid user message round-trips through validation,
    • unknown role is rejected,
    • JSON schema generation succeeds on every supported version,
    • the rich per-role input type is asserted when pydantic supports it (auto-skipped below 2.9).
  • Ran the new tests against pydantic 2.8.2 (fallback path: 3 passed, rich-schema test skipped) and 2.13.4 (rich path: 4 passed).
  • ruff check and ruff format --check clean with the repo-pinned ruff 0.14.6.

AI Disclosure

Per AI_POLICY.md: this pull request was written with AI assistance (ox-alpha coding agent operating on behalf of the submitting human) and reviewed and edited by a human, who verified the changes locally across the pydantic versions listed above. No AI co-author is recorded; the commit is DCO signed by the submitter.

…upported pydantic range

Fixes #2292.

OpenAIChatMessage passed json_schema_input_type to BeforeValidator, but
that argument is only accepted from pydantic 2.9 onwards while the project
declares pydantic>=2.5,<3.0. Importing
nemoguardrails.server.schemas.openai on pydantic 2.5-2.8 therefore raised

    TypeError: BeforeValidator.__init__() got an unexpected keyword
    argument 'json_schema_input_type'

Build the validator through a small helper that applies the argument when
the installed pydantic supports it and falls back to a plain
BeforeValidator otherwise, so the declared dependency range keeps working.
On pydantic < 2.9 the generated JSON schema describes messages as generic
objects instead of the per-role union; validation behavior is identical.

Verified against pydantic 2.8.2 (fallback path) and 2.13.4 (rich schema
path); reproduction confirmed the TypeError on 2.5.1, 2.6.4, 2.7.4 and
2.8.2 before the change.

Signed-off-by: fei <204683769+feiiiiii5@users.noreply.github.com>
@github-actions github-actions Bot added the status: needs triage New issues that have not yet been reviewed or categorized. label Aug 22, 2026
@feiiiiii5
feiiiiii5 changed the base branch from main to develop August 22, 2026 23:02
@feiiiiii5

Copy link
Copy Markdown
Author

Closing as part of a contribution-portfolio consolidation: my effort is now focused on a single project (failroute, a static analyzer for silent-failure anti-patterns) and its upstream follow-through. Happy to revisit if there is interest.

@feiiiiii5 feiiiiii5 closed this Aug 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size: XL status: needs triage New issues that have not yet been reviewed or categorized.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bug: Fix Pydantic compatibility for OpenAI chat message validator

1 participant