fix(prompt): resolve @Contextual properties in GenericJsonSchemaGenerator#1875
Merged
Andrey Bragin (EugeneTheDev) merged 2 commits intoJun 2, 2026
Conversation
Contributor
|
Hi Faraz Ahmed (@faraz152), thank you for the contribution! Please address the failing checks. |
Contributor
Author
|
Hi @aozherelyeva, the failing checks have been addressed — the ktlint indentation issue in |
When a @serializable data class contains a @contextual property, GenericJsonSchemaGenerator.process() fell into the else branch and threw an IllegalArgumentException ("Encountered unsupported type: CONTEXTUAL"), aborting schema generation before any LLM request. Fix: add an explicit SerialKind.CONTEXTUAL branch that resolves the actual descriptor from the Json instance's SerializersModule via getContextualDescriptor() and delegates to process() recursively. If no contextual serializer is registered, a descriptive error now guides the user to register one or use @serializable(with = ...). Tests: three new cases in JsonSchemaGeneratorTest covering basic and standard generators with a registered contextual serializer, and the clear-error path when the serializer is absent. Fixes JetBrains#1486
Andrey Bragin (EugeneTheDev)
force-pushed
the
fix/1486-contextual-schema-generator
branch
from
June 2, 2026 15:58
67fd3c5 to
6648c59
Compare
Andrey Bragin (EugeneTheDev)
approved these changes
Jun 2, 2026
Andrey Bragin (EugeneTheDev)
left a comment
Collaborator
There was a problem hiding this comment.
Looks good, thank you. I rebased it to the latest develop, will merge it once all checks are green
Andrey Bragin (EugeneTheDev)
merged commit Jun 2, 2026
6ee3b63
into
JetBrains:develop
24 of 26 checks passed
Andrey Bragin (EugeneTheDev)
pushed a commit
that referenced
this pull request
Jun 15, 2026
…erator (#1875) `executeStructured` crashed with `IllegalArgumentException: Encountered unsupported type while generating JSON schema: CONTEXTUAL` when the response data class contained a `@Contextual` property (e.g. `java.util.UUID`, any non-`@Serializable` type registered via `SerializersModule`). `GenericJsonSchemaGenerator.process()` had no branch for `SerialKind.CONTEXTUAL`, so it fell through to the `else` clause and threw immediately — before any LLM request was made. Added an explicit `SerialKind.CONTEXTUAL` branch that: 1. Calls `serializersModule.getContextualDescriptor(descriptor)` (stable API in kotlinx.serialization 1.7+) to resolve the actual serializer registered in the `Json` instance's module. 2. Delegates to `process()` recursively with the resolved descriptor, so the type is generated correctly (e.g. a UUID serialized as a string → `"type": "string"`). 3. If no contextual serializer is found, throws a **descriptive** `IllegalArgumentException` explaining how to fix it (register via `SerializersModule { contextual(...) }` or use `@Serializable(with = ...)`). | File | Change | |---|---| | `GenericJsonSchemaGenerator.kt` | Add `SerialKind.CONTEXTUAL` branch; `@OptIn(ExperimentalSerializationApi::class)` on class | | `JsonSchemaGeneratorTest.kt` | 3 new test cases: basic generator, standard generator, and missing-serializer error path | ``` ./gradlew :prompt:prompt-structure:jvmTest --tests "ai.koog.prompt.structure.json.generator.JsonSchemaGeneratorTest" BUILD SUCCESSFUL ``` All 3 new tests pass alongside the existing suite. Use `@Serializable(with = MySerializer::class)` on the property instead of `@Contextual`, as noted in the issue. closes #1486 (cherry picked from commit 6ee3b63)
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.
What
Fixes #1486
executeStructuredcrashed withIllegalArgumentException: Encountered unsupported type while generating JSON schema: CONTEXTUALwhen the response data class contained a@Contextualproperty (e.g.java.util.UUID, any non-@Serializabletype registered viaSerializersModule).Root cause
GenericJsonSchemaGenerator.process()had no branch forSerialKind.CONTEXTUAL, so it fell through to theelseclause and threw immediately — before any LLM request was made.Fix
Added an explicit
SerialKind.CONTEXTUALbranch that:serializersModule.getContextualDescriptor(descriptor)(stable API in kotlinx.serialization 1.7+) to resolve the actual serializer registered in theJsoninstance's module.process()recursively with the resolved descriptor, so the type is generated correctly (e.g. a UUID serialized as a string →"type": "string").IllegalArgumentExceptionexplaining how to fix it (register viaSerializersModule { contextual(...) }or use@Serializable(with = ...)).Changes
GenericJsonSchemaGenerator.ktSerialKind.CONTEXTUALbranch;@OptIn(ExperimentalSerializationApi::class)on classJsonSchemaGeneratorTest.ktTesting
All 3 new tests pass alongside the existing suite.
Workaround (before this fix)
Use
@Serializable(with = MySerializer::class)on the property instead of@Contextual, as noted in the issue.