Skip to content

fix(prompt): resolve @Contextual properties in GenericJsonSchemaGenerator#1875

Merged
Andrey Bragin (EugeneTheDev) merged 2 commits into
JetBrains:developfrom
faraz152:fix/1486-contextual-schema-generator
Jun 2, 2026
Merged

fix(prompt): resolve @Contextual properties in GenericJsonSchemaGenerator#1875
Andrey Bragin (EugeneTheDev) merged 2 commits into
JetBrains:developfrom
faraz152:fix/1486-contextual-schema-generator

Conversation

@faraz152

Copy link
Copy Markdown
Contributor

What

Fixes #1486

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).

Root cause

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.

Fix

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 = ...)).

Changes

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

Testing

./gradlew :prompt:prompt-structure:jvmTest --tests "ai.koog.prompt.structure.json.generator.JsonSchemaGeneratorTest"
BUILD SUCCESSFUL

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.

@zarechneva-a

Copy link
Copy Markdown
Contributor

Hi Faraz Ahmed (@faraz152), thank you for the contribution! Please address the failing checks.

@faraz152

Copy link
Copy Markdown
Contributor Author

Hi @aozherelyeva, the failing checks have been addressed — the ktlint indentation issue in prompt-structure has been fixed (commit 284bf14). All CI checks are now passing. Please let me know if there's anything else to address. Thanks!

@zarechneva-a Anastasiia Zarechneva (zarechneva-a) added the bugfix Something was fixed 🎉 label Jun 1, 2026
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
@EugeneTheDev
Andrey Bragin (EugeneTheDev) force-pushed the fix/1486-contextual-schema-generator branch from 67fd3c5 to 6648c59 Compare June 2, 2026 15:58

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, thank you. I rebased it to the latest develop, will merge it once all checks are green

@EugeneTheDev Andrey Bragin (EugeneTheDev) changed the title fix: resolve @Contextual properties in GenericJsonSchemaGenerator fix(prompt): resolve @Contextual properties in GenericJsonSchemaGenerator Jun 2, 2026
@EugeneTheDev
Andrey Bragin (EugeneTheDev) merged commit 6ee3b63 into JetBrains:develop Jun 2, 2026
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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bugfix Something was fixed 🎉

Projects

None yet

Development

Successfully merging this pull request may close these issues.

GenericJsonSchemaGenerator crashes on @Contextual properties in executeStructured

3 participants