@@ -269,6 +269,23 @@ def _extract_task_type(schema: dict[str, Any]) -> str | None:
269269 return const if isinstance (const , str ) else None
270270
271271
272+ # A task whose models return different shapes (upscale: image or video) gets one
273+ # type with every field, required only where every shape requires it.
274+ def _merge_shapes (shapes : list [dict [str , Any ]]) -> dict [str , Any ]:
275+ if len (shapes ) == 1 :
276+ return shapes [0 ]
277+ properties : dict [str , Any ] = {}
278+ for shape in shapes :
279+ for key , value in (shape .get ("properties" ) or {}).items ():
280+ _ = properties .setdefault (key , value )
281+ required = [
282+ key
283+ for key in shapes [0 ].get ("required" ) or []
284+ if all (key in (shape .get ("required" ) or []) for shape in shapes )
285+ ]
286+ return {** shapes [0 ], "properties" : properties , "required" : required }
287+
288+
272289def _variant_discriminator (variant : dict [str , Any ]) -> str | None :
273290 """The const value that distinguishes a oneOf variant (e.g. operation
274291 `upload`/`delete`), used to name its TypedDict. Skips taskType/taskUUID,
@@ -436,34 +453,61 @@ def add_typed_dict(class_name: str, schema: dict[str, Any], description: str) ->
436453 # generator). This is what lets a task type whose model slug differs from
437454 # the taskType — e.g. `training` (slug exactly-illustrative-training,
438455 # taskType training) — still get an importable canonical result.
439- seen_task_type_results : set [str ] = set ()
440- for entries , skip in (
441- (inference , {"authentication" , "getResponse" , "ping" }),
442- (utilities , {"authentication" , "ping" }),
443- ):
444- for entry in entries :
456+ #
457+ # A model's own response drops fields its shared type declares when the
458+ # model never returns them (seed on a model that takes none), so an
459+ # inference task's result comes from the shared types its models reference,
460+ # all of them merged. A bundle without responseTypes only has each model's
461+ # own response, and the first one stands for the task.
462+ inference_skip = {"authentication" , "getResponse" , "ping" }
463+ result_shapes : dict [str , list [dict [str , Any ]]] = {}
464+ if response_types is not None :
465+ slugs_by_task : dict [str , set [str ]] = {}
466+ for entry in inference :
467+ slug = entry .get ("responseType" )
468+ shape = response_types .get (slug ) if isinstance (slug , str ) else None
469+ task_type = _extract_task_type (shape ) if isinstance (shape , dict ) else None
470+ if not isinstance (slug , str ) or task_type is None or task_type in inference_skip :
471+ continue
472+ slugs_by_task .setdefault (task_type , set ()).add (slug )
473+ for task_type , slugs in slugs_by_task .items ():
474+ result_shapes [task_type ] = [response_types [slug ] for slug in sorted (slugs )]
475+ else :
476+ for entry in inference :
445477 response_schema = entry .get ("responseSchema" )
446- # A model's own response drops fields its shared type declares when
447- # the model never returns them (seed on a model that takes none), so
448- # the canonical result comes from the shared type it references.
449- if entries is inference and response_types is not None :
450- response_type = entry .get ("responseType" )
451- response_schema = response_types .get (response_type ) if isinstance (response_type , str ) else None
452478 if not isinstance (response_schema , dict ):
453479 continue
454- result_task_type = _extract_task_type (response_schema ) or _extract_task_type (entry .get ("requestSchema" ) or {})
455- if (
456- not isinstance (result_task_type , str )
457- or result_task_type in seen_task_type_results
458- or result_task_type in skip
459- ):
480+ task_type = _extract_task_type (response_schema )
481+ if task_type is None or task_type in result_shapes or task_type in inference_skip :
460482 continue
461- seen_task_type_results .add (result_task_type )
462- _ = add_typed_dict (
463- to_class_name (result_task_type , "Result" ),
464- response_schema ,
465- f"Canonical result shape for `{ result_task_type } ` tasks." ,
466- )
483+ result_shapes [task_type ] = [response_schema ]
484+
485+ seen_task_type_results : set [str ] = set ()
486+ for task_type , shapes in sorted (result_shapes .items ()):
487+ seen_task_type_results .add (task_type )
488+ _ = add_typed_dict (
489+ to_class_name (task_type , "Result" ),
490+ _merge_shapes (shapes ),
491+ f"Canonical result shape for `{ task_type } ` tasks." ,
492+ )
493+
494+ for entry in utilities :
495+ response_schema = entry .get ("responseSchema" )
496+ if not isinstance (response_schema , dict ):
497+ continue
498+ result_task_type = _extract_task_type (response_schema ) or _extract_task_type (entry .get ("requestSchema" ) or {})
499+ if (
500+ not isinstance (result_task_type , str )
501+ or result_task_type in seen_task_type_results
502+ or result_task_type in {"authentication" , "ping" }
503+ ):
504+ continue
505+ seen_task_type_results .add (result_task_type )
506+ _ = add_typed_dict (
507+ to_class_name (result_task_type , "Result" ),
508+ response_schema ,
509+ f"Canonical result shape for `{ result_task_type } ` tasks." ,
510+ )
467511
468512 # ----- registry dictionaries -----
469513
0 commit comments