Coverage for app / services / bedrock_schema.py: 100%

18 statements  

« prev     ^ index     » next       coverage.py v7.13.4, created at 2026-10-31 11:23 +0000

1""" 

2Amazon Bedrock's structured outputs: the JSON Schema it decodes to natively. 

3 

4Bedrock constrains a reply to a JSON schema (Converse ``outputConfig. 

5textFormat``) for the models whose card lists "Structured outputs" — on the 

6catalogue's routes, Claude Haiku 4.5, Sonnet 4.6 and Opus 4.6, gpt-oss-120b 

7and Qwen3 235B; not the Claude 5.x models (each route's 

8``native_structured_output``). It accepts a subset of JSON Schema Draft 

92020-12 and answers a schema outside it with a 400 (docs: 

10``bedrock/latest/userguide/structured-output.html``, checked 2026-10-19): 

11 

12* supported — every basic type, ``enum`` of strings / numbers / booleans / 

13 null, ``const``, ``anyOf``, ``allOf``, internal ``$ref`` / ``$defs`` / 

14 ``definitions``, a fixed list of string ``format`` values, and ``minItems`` 

15 of 0 or 1; 

16* not supported — recursion, external ``$ref``, ``minimum`` / ``maximum`` / 

17 ``multipleOf``, ``minLength`` / ``maxLength``, and ``additionalProperties`` 

18 other than ``false``. 

19 

20Two paths use it. ``structured_call``'s native path sends 

21``structured_output.strict_schema``, which keeps a smaller set of keywords 

22than this; a test holds every model-facing schema inside it, so no native 

23call is refused for its shape. The LiteLLM path (``llm.run_model``) sends a 

24caller's ``output_schema``, which can say anything: ``bedrock_json_schema`` 

25reduces it to what Bedrock takes — the instruction in the prompt still states 

26the whole schema, and the caller validates the reply against it. 

27 

28A new schema is compiled into a grammar on first use, which "may take up to a 

29few minutes", then cached for 24 hours per account; the Bedrock routes that 

30decode natively get the long timeout (``model_capabilities``). 

31""" 

32 

33from __future__ import annotations 

34 

35import json 

36 

37from app.util import safe_json 

38 

39# The subset itself is shared with svc-core (``app.util.model_output``), 

40# which holds its own model-facing schemas to it; re-exported here. 

41from app.util.model_output import BEDROCK_FORMATS as FORMATS # noqa: F401 

42from app.util.model_output import BEDROCK_KEYWORDS as KEYWORDS # noqa: F401 

43from app.util.model_output import bedrock_json_schema 

44from app.util.model_output import bedrock_problems as problems # noqa: F401 

45 

46 

47def output_config(output_schema: str | None, name: str = "output") -> dict | None: 

48 """Converse's ``outputConfig`` for a caller's ``output_schema``, or None 

49 when it is not a JSON Schema object (a prose description of the shape, a 

50 YAML layout): the instruction alone asks for it then.""" 

51 if not output_schema or not output_schema.strip(): 

52 return None 

53 try: 

54 # Strict, not lenient: this is configuration, and a schema that does 

55 # not parse is a description of a shape, not a schema. 

56 parsed = json.loads(output_schema) 

57 except (TypeError, ValueError): 

58 return None 

59 if not isinstance(parsed, dict) or parsed.get("type") != "object": 

60 return None 

61 reduced = bedrock_json_schema(parsed) 

62 return { 

63 "textFormat": { 

64 "type": "json_schema", 

65 "structure": { 

66 "jsonSchema": { 

67 "schema": safe_json.dumps(reduced, ensure_ascii=False), 

68 "name": name, 

69 } 

70 }, 

71 } 

72 }