﻿# OpenAPI unions: a discriminator does not repair overlapping oneOf branches

Distinguish oneOf validation from discriminator-based branch selection, constrain branch tags explicitly, and compare schema rules with runtime union behavior.

Author: Retrivium editorial team
Updated: 2026-11-04
Canonical URL: https://retrivium.ai/journal/guide-openapi-oneof-discriminator-validation/
Language: en

## OneOf requires one matching branch

JSON Schema defines oneOf as successful when the instance matches exactly one subschema. Matching several branches is a validation failure, even if a client can choose one convenient class to deserialize the object.

For an AI API carrying text, tool and media records, optional fields alone may leave variants overlapping. A proposed schema review asks which actual constraints distinguish each record type. Class names in generated documentation do not create that distinction in the submitted JSON.

## Discriminator has a separate role

The inspected OpenAPI specification states that discriminator must not change the schema's validation outcome. It can help tooling identify a variant, but it does not replace the constraints that make that variant valid and the alternatives invalid.

A consumer should not assume that adding a discriminator fixes an ambiguous oneOf. Review the tag property's presence and its permitted value in each branch. If a branch accepts any tag or does not require one, the intended mutually exclusive contract may not be expressed by the schema.

## Tag mappings need explicit interpretation

A discriminator mapping associates tag values with schema references. That association helps a tool locate the intended variant. It is different from the instance constraints that decide whether the record actually satisfies the selected schema.

A proposed compatibility matrix includes each known tag, a missing tag, an unknown tag and a tag paired with another variant's fields. These cases expose whether the server and client agree about selection and validity rather than only agreeing on the happy-path example shown in documentation.

## Runtime unions can select by another algorithm

Pydantic documents left-to-right, smart and discriminated union approaches. An untagged runtime union can select a matching member without implementing JSON Schema's exactly-one condition. The framework also reserves changes to its smart matching algorithm.

Preserve the runtime mode alongside the published schema. A record accepted and converted by a model class is not by itself evidence that an independent oneOf validator accepts the same original object. Conversion and validation may have different rules and produce different diagnostics.

## Review accepted inputs before generated types

A generated type hierarchy can make variants look clean while the underlying wire contract remains ambiguous. Compare accepted examples and rejected boundary cases before treating generated classes as proof of a well-defined union.

The proposed review connects branch constraints, discriminator mapping and runtime selection to the same records. This makes variant handling understandable without promising that a discriminator enforces every constraint, that a class hierarchy is inheritance on the wire or that all generated clients select identical branches.

## Primary references

[JSON Schema boolean combinations](https://json-schema.org/understanding-json-schema/reference/combining).

[OpenAPI discriminator definition](https://spec.openapis.org/oas/v3.2.0.html).

[Pydantic union modes](https://pydantic.dev/docs/validation/latest/concepts/unions/).

Related explanations: [Schema conformance](/journal/provider-structured-output-conformance-tests/); [Schema conversion](/journal/llama-cpp-json-schema-gbnf-validation/).
