google / google/dotprompt

Picoschema.isPicoschema returns false for the 'type, description' form, so common picoschemas aren't detected

Open
#565 0 comments 0 reactions 1 assignee Claimed by @pavelgj View on GitHub
Dominant language
Starlark
Stars
563
Forks
68
Avg merge
6m
Merged PRs (30d)
2

Description

## Summary

`Picoschema.isPicoschema` (Dart, `dotprompt-0.0.1`, `lib/src/picoschema.dart`) does not recognize the `type, description` field form that the docs and examples use throughout, e.g.:

```yaml
input:
schema:
name: string, the person to greet
age: integer, their age
```

`isPicoschema` returns `false` for this schema, so any code that gates conversion on it (including `_resolveMetadata` in `lib/src/dotprompt.dart`) skips conversion and leaves the raw picoschema in place. `Picoschema.toJsonSchema` itself handles this form correctly — only the *detection* is wrong.

> Note: this is in the Dart port (`dotprompt` 0.0.1 on pub.dev). If detection is handled differently in the canonical implementations, please point me at the right place.

## Details

`isPicoschema` only returns `true` when a field value is a string matching a bare primitive type, an array pattern (`string[]`), or an enum pattern (`a | b | c`). A value like `"string, the person to greet"` matches none of those checks, so the loop falls through to the default `return false`:

```dart
for (final value in schema.values) {
if (value is String) {
final normalized = value.toLowerCase();
if (_primitiveTypes.containsKey(normalized) ||
_arrayPattern.hasMatch(value) ||
_enumPattern.hasMatch(value)) {
return true;
}
}
}
return false; // <- reached for `type, description` form
```

By contrast, `_parseTypeString` (used by `toJsonSchema`) explicitly handles the comma form:

```dart
final commaIndex = trimmed.indexOf(",");
if (commaIndex > 0) {
final typePart = trimmed.substring(0, commaIndex).trim();
final descPart = trimmed.substring(commaIndex + 1).trim();
...
}
```

So detection and conversion disagree on the same input.

## Reproduction

```dart
import 'package:dotprompt/dotprompt.dart';

void main() {
final schema = {'name': 'string, the person to greet'};
print(Picoschema.isPicoschema(schema)); // false (expected: true)
print(Picoschema.toJsonSchema(schema)); // correctly converts
}
```

## Impact

Anything that uses `isPicoschema` as a conversion guard silently skips the most common documented picoschema form. In our case (Genkit Dart's file-based prompt loader) it meant `input`/`output` schemas declared with descriptions were never converted to JSON Schema and the raw picoschema reached the model as the response schema. We worked around it by detecting "already JSON Schema" directly instead of relying on `isPicoschema`, but the detection itself looks buggy.

## Suggested fix

Treat a string value containing a `, ` form (a recognized type before the first comma) as picoschema in `isPicoschema` — i.e. mirror the cases `_parseTypeString` already handles. More broadly, detection and conversion should share the same notion of what a valid picoschema type string is.

## Environment

- `dotprompt` 0.0.1 (Dart, pub.dev)
- Dart 3.x

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.