Skip to content

Commit 2a7f8f6

Browse files
committed
fix(gemini): emit only typed element schemas and translate type lists for the Schema proto
The `parameters` field is Gemini's `Schema` proto, where `type` is REQUIRED on every node, nullability is its own `nullable` field, and neither `prefixItems` nor a JSON `type` list exists. The first cut folded a heterogeneous tuple into `items: {anyOf: [...]}`, a node with no `type`, which is the shape Google's own bridges collapse rather than forward (google/adk-python#3424, googleapis/python-genai#1807), so the captured `[field, operator, value]` case would still have failed. - Tuple positions now collapse to one typed schema: the single schema when they agree, their shared `type` when they agree on that much, otherwise the first position's schema. Array positions keep a complete branch so the derived `items` never lacks its own `items`. - A position contributes its type directly, through `anyOf`/`oneOf` arms, or through an `enum` of uniformly typed values; a typeless position contributes nothing. - A `prefixItems`-only tuple (no `type`) is recognized as an array and gains `type: "array"` (cubic P2 on #454); draft-07 array-valued `items` is folded the same way and a boolean `items` is replaced. - A JSON `type` list becomes the scalar type plus `nullable: true`, mirroring CLIProxyAPI's flattenTypeArrays. - `properties`/`$defs`/`definitions` values are walked as schemas while their keys are left alone; `default`, `example(s)` and `enum` hold instances and are not walked. - The array fix runs after the child walk so folded positions are already sanitized.
1 parent 06a257a commit 2a7f8f6

2 files changed

Lines changed: 387 additions & 38 deletions

File tree

‎src/model/gemini_request.rs‎

Lines changed: 185 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -351,10 +351,32 @@ fn sanitize_gemini_schema(value: &mut Value, depth: usize) -> Result<(), Adapter
351351
] {
352352
map.remove(key);
353353
}
354-
give_array_schema_items(map);
355-
for child in map.values_mut() {
356-
sanitize_gemini_schema(child, depth + 1)?;
354+
for (key, child) in map.iter_mut() {
355+
match key.as_str() {
356+
// Maps of schemas keyed by names the *tool* chose. The
357+
// values are schemas; the keys are not keywords, so a
358+
// property called `const` or `enum` must be neither
359+
// stripped nor skipped on account of its name.
360+
"properties" | "$defs" | "definitions" => {
361+
if let Value::Object(schemas) = child {
362+
for schema in schemas.values_mut() {
363+
sanitize_gemini_schema(schema, depth + 1)?;
364+
}
365+
}
366+
}
367+
// `default`, `example(s)` and `enum` hold *instances*, not
368+
// schemas. Walking them would strip keys from — and, worse,
369+
// invent an `items` inside — data the tool declared verbatim.
370+
"default" | "example" | "examples" | "enum" => {}
371+
_ => sanitize_gemini_schema(child, depth + 1)?,
372+
}
357373
}
374+
// After the children, so the tuple positions folded into `items`
375+
// below are already sanitized: an element schema that is itself an
376+
// array has its own `items` by now, and two positions that differ
377+
// only in a key just stripped are seen as the duplicates they are.
378+
flatten_type_list(map);
379+
give_array_schema_items(map);
358380
}
359381
Value::Array(array) => {
360382
for child in array {
@@ -373,45 +395,182 @@ fn sanitize_gemini_schema(value: &mut Value, depth: usize) -> Result<(), Adapter
373395
/// The backend validates function declarations against its own `Schema`
374396
/// message, and there an array without `items` is a hard `400` for the whole
375397
/// request (`…properties[where].items.items: missing field.`). Tools written
376-
/// against JSON Schema 2020-12 describe a fixed-shape array —
377-
/// `[field, operator, value]` — with `prefixItems` and no `items`, which is
378-
/// exactly that case. `prefixItems` is positional and has no Gemini
379-
/// counterpart, so it becomes one `items` schema that admits any of the
380-
/// declared positions: the single schema when they all agree, `anyOf` over
381-
/// the distinct ones otherwise. A position that declares no `type` at all
382-
/// (the JSON Schema "anything" slot) contributes nothing, since a typeless
383-
/// branch is the same missing-field failure one level down; when no position
384-
/// carries a type — or the array declares nothing about its elements — the
385-
/// element is declared a string. That last resort trades precision for a
386-
/// request that reaches the model at all, and is the least surprising type for
387-
/// an element nothing described.
398+
/// against JSON Schema describe a fixed-shape array — `[field, operator,
399+
/// value]` — as a tuple: `prefixItems` in 2020-12, an array-valued `items` in
400+
/// draft-07. Neither is a Gemini `Schema` and neither leaves the array with the
401+
/// single `items` schema it needs, so the positions are folded into one.
402+
///
403+
/// `Schema` also declares `type` REQUIRED on *every* node and has no union of
404+
/// its own, so the folded schema has to carry a type: an `anyOf` node with no
405+
/// `type` is the same missing-field failure one level down, and it is the shape
406+
/// Google's own bridges collapse rather than forward. A position contributes
407+
/// the type it declares — directly, through `anyOf`/`oneOf` arms, or through an
408+
/// `enum` of uniformly typed values; a position that declares nothing (the JSON
409+
/// Schema "anything" slot) contributes nothing. The contributions collapse to
410+
/// the single schema when they agree, to their shared `type` when they agree on
411+
/// that much, and otherwise to the first position's schema. When no position
412+
/// carries a type — or the array declares nothing about its elements at all —
413+
/// the element is declared a string, matching CLIProxyAPI's fallback (see
414+
/// `.please/docs/research/md/003-json-schema-sanitization-for-gemini-functiondeclar.md`).
415+
/// Those last resorts trade precision for a request that reaches the model at
416+
/// all: the cost is a narrowed element type, not a rejected request.
417+
///
418+
/// A tuple that declares `prefixItems` and no `type` at all is an array in
419+
/// everything but name — JSON Schema applies `prefixItems` only to array
420+
/// instances, and a tool author who wrote one meant a tuple — so it is given
421+
/// `type: "array"` before the same derivation runs, rather than losing the
422+
/// tuple and reaching the backend as a schema that says nothing.
388423
fn give_array_schema_items(map: &mut Map<String, Value>) {
389-
let prefix_items = map.remove("prefixItems");
390-
if !is_array_schema(map) || map.contains_key("items") {
424+
// Established before anything is removed: every other object in a schema
425+
// tree — a `properties` container above all — must leave here untouched.
426+
if !is_array_schema(map) {
391427
return;
392428
}
429+
// Positional and without a Gemini counterpart: folded in below, or dropped
430+
// when `items` already says what the elements are.
431+
let prefix_items = map.remove("prefixItems");
432+
map.entry("type").or_insert_with(|| json!("array"));
433+
// `items` counts as already answered only when it is a schema object.
434+
// draft-07 spells a tuple as an array there and 2020-12 closes one with
435+
// `false`; neither is a schema Gemini can read.
436+
let legacy_tuple = match map.get("items") {
437+
Some(Value::Object(_)) => return,
438+
Some(Value::Array(_)) => map.remove("items"),
439+
Some(_) => {
440+
map.remove("items");
441+
None
442+
}
443+
None => None,
444+
};
393445
let mut branches: Vec<Value> = Vec::new();
394-
if let Some(Value::Array(prefix)) = prefix_items {
395-
for schema in prefix {
396-
let typed = schema.get("type").is_some() || schema.get("anyOf").is_some();
397-
if typed && !branches.contains(&schema) {
398-
branches.push(schema);
399-
}
446+
if let Some(Value::Array(positions)) = prefix_items.or(legacy_tuple) {
447+
for position in positions {
448+
collect_typed_branches(position, &mut branches);
400449
}
401450
}
402451
let items = match branches.len() {
403452
0 => json!({ "type": "string" }),
404453
1 => branches.remove(0),
405-
_ => json!({ "anyOf": branches }),
454+
_ => merge_branches(branches),
406455
};
407456
map.insert("items".to_string(), items);
408457
}
409458

459+
/// Add the schemas one tuple position admits, skipping the ones that would
460+
/// leave a branch without the `type` Gemini requires.
461+
fn collect_typed_branches(position: Value, branches: &mut Vec<Value>) {
462+
let Some(map) = position.as_object() else {
463+
return;
464+
};
465+
if map.contains_key("type") {
466+
push_distinct(branches, position);
467+
return;
468+
}
469+
if let Some(Value::Array(arms)) = map.get("anyOf").or_else(|| map.get("oneOf")) {
470+
for arm in arms.clone() {
471+
collect_typed_branches(arm, branches);
472+
}
473+
return;
474+
}
475+
if let Some(kind) = enum_value_type(map.get("enum")) {
476+
push_distinct(branches, json!({ "type": kind }));
477+
}
478+
}
479+
480+
fn push_distinct(branches: &mut Vec<Value>, schema: Value) {
481+
if !branches.contains(&schema) {
482+
branches.push(schema);
483+
}
484+
}
485+
486+
/// The one `type` an `enum` of uniformly typed values implies, so a position
487+
/// constrained by an enum alone is not mistaken for the "anything" slot.
488+
fn enum_value_type(values: Option<&Value>) -> Option<&'static str> {
489+
let Value::Array(values) = values? else {
490+
return None;
491+
};
492+
let mut kind: Option<&'static str> = None;
493+
for value in values {
494+
let this = match value {
495+
Value::String(_) => "string",
496+
Value::Number(_) => "number",
497+
Value::Bool(_) => "boolean",
498+
_ => return None,
499+
};
500+
match kind {
501+
None => kind = Some(this),
502+
Some(seen) if seen == this => {}
503+
Some(_) => return None,
504+
}
505+
}
506+
kind
507+
}
508+
509+
/// Collapse several element schemas into the one typed schema Gemini reads.
510+
fn merge_branches(mut branches: Vec<Value>) -> Value {
511+
if let Some(shared) = branches[0].get("type").cloned() {
512+
// A bare `array` is the one type that is no schema on its own — it
513+
// needs `items` — so array positions keep the first, already
514+
// sanitized, branch instead of collapsing to the type.
515+
if shared != json!("array")
516+
&& branches
517+
.iter()
518+
.all(|branch| branch.get("type") == Some(&shared))
519+
{
520+
return json!({ "type": shared });
521+
}
522+
}
523+
branches.remove(0)
524+
}
525+
526+
/// Rewrite a JSON Schema type *list* as the scalar type plus `nullable`.
527+
///
528+
/// `Schema.type` is a single `Type` enum and nullability is its own `nullable`
529+
/// field, so `{"type": ["array", "null"]}` is not a value the `parameters`
530+
/// field can take — it is a `400` on `type` however good the rest of the
531+
/// schema is. The list collapses the way the reference bridges collapse it
532+
/// (CLIProxyAPI's `flattenTypeArrays`): the first non-`null` entry becomes the
533+
/// type, and `null` among the entries becomes `nullable: true`. A list that
534+
/// names nothing but `null` is simply that type; one that names nothing usable
535+
/// is left alone rather than guessed at.
536+
fn flatten_type_list(map: &mut Map<String, Value>) {
537+
let Some(Value::Array(kinds)) = map.get("type") else {
538+
return;
539+
};
540+
let mut nullable = false;
541+
let mut scalar: Option<Value> = None;
542+
for kind in kinds {
543+
match kind.as_str() {
544+
Some("null") => nullable = true,
545+
Some(_) if scalar.is_none() => scalar = Some(kind.clone()),
546+
_ => {}
547+
}
548+
}
549+
match scalar {
550+
Some(kind) => {
551+
map.insert("type".to_string(), kind);
552+
if nullable {
553+
map.insert("nullable".to_string(), json!(true));
554+
}
555+
}
556+
None if nullable => {
557+
map.insert("type".to_string(), json!("null"));
558+
}
559+
None => {}
560+
}
561+
}
562+
410563
fn is_array_schema(map: &Map<String, Value>) -> bool {
411564
match map.get("type") {
412565
Some(Value::String(kind)) => kind == "array",
413-
Some(Value::Array(kinds)) => kinds.iter().any(|kind| kind == "array"),
414-
_ => false,
566+
// A type list has been flattened to a scalar by the time this runs.
567+
Some(_) => false,
568+
// `prefixItems` constrains arrays and nothing else, so a tuple that
569+
// omits `type` — which JSON Schema allows — is still an array schema.
570+
// A *list* of positions, specifically: a schema keyed under a property
571+
// named `prefixItems` is an object, and must not turn its `properties`
572+
// container into an array.
573+
None => matches!(map.get("prefixItems"), Some(Value::Array(_))),
415574
}
416575
}
417576

0 commit comments

Comments
 (0)