Skip to content

add structured output test#2817

Open
milaGGL wants to merge 1 commit into
masterfrom
test-structured-output
Open

add structured output test#2817
milaGGL wants to merge 1 commit into
masterfrom
test-structured-output

Conversation

@milaGGL

@milaGGL milaGGL commented Jul 9, 2026

Copy link
Copy Markdown

No description provided.

@wiz-9635d3485b

wiz-9635d3485b Bot commented Jul 9, 2026

Copy link
Copy Markdown

Wiz Scan Summary

Scanner Findings
Vulnerability Finding Vulnerabilities 2 High 2 Medium 4 Low
Data Finding Sensitive Data -
Secret Finding Secrets -
IaC Misconfiguration IaC Misconfigurations -
SAST Finding SAST Findings -
Software Management Finding Software Management Findings -
Total 2 High 2 Medium 4 Low

View scan details in Wiz

To detect these findings earlier in the dev lifecycle, try using Wiz Code VS Code Extension.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request downgrades Gradle and several dependency versions (including Kotlin and AGP) and introduces a new "Structured & Hybrid Output" demo feature. The demo showcases structured output generation using both KSP-generated schemas and manual JSON schemas across cloud, on-device, and hybrid modes. Feedback focuses on improving the robustness of JSON deserialization from LLM outputs: specifically, extracting JSON blocks more safely, ignoring unknown keys, and providing default values in the MovieReview data class to prevent serialization crashes.

Comment on lines +123 to +127
val cleanJson = rawTextString
.replace("```json", "")
.replace("```", "")
.trim()
parsedObject = Json.decodeFromString<MovieReview>(cleanJson)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

LLM outputs are inherently unpredictable and can occasionally include conversational preamble, postamble, or markdown code blocks even when instructed to return JSON. Additionally, the model might return extra fields not defined in your schema. To prevent SerializationException crashes, configure the Json parser to ignore unknown keys and use a more robust extraction method to isolate the JSON block (between the first { and last }).

                            val jsonStartIndex = rawTextString.indexOf('{')
                            val jsonEndIndex = rawTextString.lastIndexOf('}')
                            val cleanJson = if (jsonStartIndex != -1 && jsonEndIndex != -1 && jsonStartIndex < jsonEndIndex) {
                                rawTextString.substring(jsonStartIndex, jsonEndIndex + 1)
                            } else {
                                rawTextString
                            }
                            val jsonParser = Json { ignoreUnknownKeys = true }
                            parsedObject = jsonParser.decodeFromString<MovieReview>(cleanJson)

Comment on lines +9 to +18
data class MovieReview(
@Guide(description = "The official title of the movie or TV show")
val title: String,
@Guide(description = "A concise 1-sentence summary of the critique")
val summary: String,
@Guide(description = "The rating out of 5 stars (1 to 5)")
val rating: Int,
@Guide(description = "Key genre tags or themes")
val tags: List<String>
) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

When deserializing structured outputs from LLMs, it is highly recommended to provide default values for all properties. LLMs (especially smaller or on-device models) can occasionally omit fields or return incomplete JSON structures. Providing default values ensures that the parser can fall back gracefully instead of throwing a SerializationException.

Suggested change
data class MovieReview(
@Guide(description = "The official title of the movie or TV show")
val title: String,
@Guide(description = "A concise 1-sentence summary of the critique")
val summary: String,
@Guide(description = "The rating out of 5 stars (1 to 5)")
val rating: Int,
@Guide(description = "Key genre tags or themes")
val tags: List<String>
) {
data class MovieReview(
@Guide(description = "The official title of the movie or TV show")
val title: String = "",
@Guide(description = "A concise 1-sentence summary of the critique")
val summary: String = "",
@Guide(description = "The rating out of 5 stars (1 to 5)")
val rating: Int = 0,
@Guide(description = "Key genre tags or themes")
val tags: List<String> = emptyList()
) {

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant