fix: set additionalProperties: false for pydantic models with extra='ignore'#1458
Open
octo-patch wants to merge 1 commit into
Open
Conversation
…gnore When using pydantic BaseModel (with default or explicit extra='ignore') as a json() schema, the LLM could previously generate arbitrary extra fields that would be silently discarded by pydantic validation. This wastes tokens and surprises users expecting only declared fields. This patch extends GenerateJsonSchemaSafe to override model_schema() and add additionalProperties: false whenever the model's extra_fields_behavior is None (implicit default) or ignore (explicitly set). Models with extra='allow' retain their flexible schemas; extra='forbid' is already handled by pydantic. The change applies recursively to nested pydantic models as well. Closes guidance-ai#1050 Related: guidance-ai#1125
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Fixes #1050
Related: #1125
Problem
When using a pydantic
BaseModel(with the defaultextra='ignore'or explicitextra='ignore') as the schema forguidance.json(), the generated JSON grammar allows the LLM to produce arbitrary additional fields that pydantic would silently discard during validation.This behaviour wastes tokens and surprises users who expect only the declared fields to be generated, as reported in #1050 and #1125.
Solution
Override
model_schema()inGenerateJsonSchemaSafeto add"additionalProperties": falsewhenever the model'sextra_fields_behavioris:None— the implicit pydantic default, which behaves asextra='ignore'"ignore"— explicitly setModels with
extra='allow'retain flexible schemas (pydantic setsadditionalProperties: true).Models with
extra='forbid'already receiveadditionalProperties: falsefrom pydantic itself.The fix applies recursively to nested pydantic models within
$defs, since each model'smodel_schema()is called independently.Before / After
Testing
Added
TestAdditionalPropertiestotests/unit/library/test_pydantic.pycovering:extrarejects additional propertiesextra='ignore'rejects additional propertiesextra='allow'still permits additional properties