fix(llm): apply tool defaults across schema shapes - #2150
Open
rosetta-livekit-bot[bot] wants to merge 1 commit into
Open
fix(llm): apply tool defaults across schema shapes#2150rosetta-livekit-bot[bot] wants to merge 1 commit into
rosetta-livekit-bot[bot] wants to merge 1 commit into
Conversation
🦋 Changeset detectedLatest commit: a9d9d9f The changes in this PR will be included in the next version bump. This PR includes changesets to release 39 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
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.
Summary
@livekit/agentsSource diff coverage
Coverage
livekit-agents/livekit/agents/llm/_strict.py: adapted toagents/src/llm/zod-utils.ts; target strict schema generation is Zod/JSON Schema based, so the source null-sentinel/default-removal behavior was ported intozodSchemaToJsonSchema(..., strict: true)and tested with target Zod schemas.livekit-agents/livekit/agents/llm/utils.py: adapted toagents/src/llm/utils.ts; target tool execution validates through Zod, so the source recursive default injection and nullability checks were ported beforeparseZodSchema.tests/test_response_format.py: not applicable as a standalone target file; agents-js has no equivalent structured response-format helper orvalidate_response_formatdecode path. The sentinel-free response-format intent is covered by non-strict schema generation tests inagents/src/llm/zod-utils.test.ts.tests/test_tools.py: adapted toagents/src/llm/utils.test.tsandagents/src/llm/zod-utils.test.ts; source tool default/sentinel coverage was ported to target tool execution and strict Zod schema tests.Target Gap
Tests
pnpm test agentspnpm test agents/src/llm/zod-utils.test.ts agents/src/llm/utils.test.tspnpm --filter @livekit/agents buildpnpm --filter @livekit/agents typecheckpnpm --filter @livekit/agents lint(passes with existing warnings)No
cue-clirun: this port touches LLM/tool schema handling, not the voice pipeline.Ported from livekit/agents#6444
Original PR description
Summary
Strict JSON schemas require every property and don't support the
defaultkeyword. This PR settles how defaulted fields are handled on each of the two encode paths.Tool schemas keep the null sentinel, and it now works for every schema shape. A defaulted field additionally accepts
null, meaning "use the Python default" — the LLM never has to invent a value it can't know. This is safe because the framework controls the decode:_prepare_function_argumentsresolves the sentinel before validation.Literal(const/enum), unions (anyOf), and$ref-typed defaults (nested models, enums) previously either contradicted themselves or never became nullable.dict[str, Model]values, and tuple items are decoded (previously a top-level-only signature walk).properties, matching the closed-object semantics of the strict wire schema — prevents a payload meant for variant B from silently matching variant A and dropping data.Literal["x", None]) are preserved instead of being replaced with the default.$ref-typed field (e.g. a documented nested-model param), sibling keys likedescriptionstay on the wrapper — strict mode forbids siblings on$refbut allows them next toanyOf.Response format schemas get no sentinel.
to_openai_response_formatoutput is decoded by users with plain Pydantic validation, so the wire schema must be exactly what validation accepts. Defaulted fields stay required and non-nullable; the model produces their values and the Python default never fires.to_strict_json_schematakes anull_sentinel_for_defaultsflag (tools pass it; response formats don't).Behavior changes
response_format: the model can no longer sendnullfor a defaulted non-nullable field. Previously bare-type defaulted fields were widened to["type", "null"], producing payloads that plainmodel_validaterejects.nullfor a required parameter with no default now surfaces as a PydanticValidationError(still wrapped inToolError) instead of a hand-writtenValueError.Known limitation
Field(default_factory=...)emits no schemadefault, so factory-defaulted tool fields get no sentinel and remain required on the wire. Left for a follow-up.Testing