structa-maestro 0.2.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (55) hide show
  1. structa_maestro/__init__.py +294 -0
  2. structa_maestro/batch.py +564 -0
  3. structa_maestro/binding.py +206 -0
  4. structa_maestro/builder/__init__.py +0 -0
  5. structa_maestro/builder/result_builder.py +308 -0
  6. structa_maestro/cli.py +730 -0
  7. structa_maestro/confidence/__init__.py +49 -0
  8. structa_maestro/confidence/calibration.py +990 -0
  9. structa_maestro/confidence/normalizer.py +203 -0
  10. structa_maestro/config.py +111 -0
  11. structa_maestro/contracts/__init__.py +0 -0
  12. structa_maestro/contracts/enums.py +86 -0
  13. structa_maestro/contracts/models.py +229 -0
  14. structa_maestro/contracts/serialization.py +169 -0
  15. structa_maestro/documents/__init__.py +32 -0
  16. structa_maestro/documents/docx_parser.py +215 -0
  17. structa_maestro/documents/eml_parser.py +360 -0
  18. structa_maestro/documents/html_parser.py +337 -0
  19. structa_maestro/documents/inputs.py +277 -0
  20. structa_maestro/documents/loader.py +675 -0
  21. structa_maestro/documents/markdown_parser.py +225 -0
  22. structa_maestro/documents/models.py +33 -0
  23. structa_maestro/documents/normalization.py +24 -0
  24. structa_maestro/documents/pdf_parser.py +186 -0
  25. structa_maestro/documents/txt_parser.py +169 -0
  26. structa_maestro/engine.py +1201 -0
  27. structa_maestro/exceptions.py +38 -0
  28. structa_maestro/provenance/__init__.py +0 -0
  29. structa_maestro/provenance/associator.py +234 -0
  30. structa_maestro/py.typed +0 -0
  31. structa_maestro/review.py +1234 -0
  32. structa_maestro/schema/__init__.py +19 -0
  33. structa_maestro/schema/loader.py +616 -0
  34. structa_maestro/schema/models.py +269 -0
  35. structa_maestro/schema/pydantic_adapter.py +566 -0
  36. structa_maestro/schema/rules.py +526 -0
  37. structa_maestro/schema/serializer.py +156 -0
  38. structa_maestro/strategies/__init__.py +45 -0
  39. structa_maestro/strategies/base.py +101 -0
  40. structa_maestro/strategies/llm_anthropic.py +1142 -0
  41. structa_maestro/strategies/llm_config.py +154 -0
  42. structa_maestro/strategies/llm_gemini.py +1171 -0
  43. structa_maestro/strategies/llm_helpers.py +240 -0
  44. structa_maestro/strategies/llm_openai_compat.py +1183 -0
  45. structa_maestro/strategies/schema_compiler.py +248 -0
  46. structa_maestro/strategies/testing.py +71 -0
  47. structa_maestro/telemetry.py +800 -0
  48. structa_maestro/validation/__init__.py +0 -0
  49. structa_maestro/validation/cross_field.py +463 -0
  50. structa_maestro/validation/validator.py +531 -0
  51. structa_maestro-0.2.0.dist-info/METADATA +394 -0
  52. structa_maestro-0.2.0.dist-info/RECORD +55 -0
  53. structa_maestro-0.2.0.dist-info/WHEEL +4 -0
  54. structa_maestro-0.2.0.dist-info/entry_points.txt +2 -0
  55. structa_maestro-0.2.0.dist-info/licenses/LICENSE +21 -0
@@ -0,0 +1,154 @@
1
+ """Immutable configuration models and enums for OpenAICompatibleLLMStrategy."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import math
6
+ from dataclasses import dataclass
7
+ from enum import StrEnum
8
+
9
+ from structa_maestro.exceptions import ConfigurationError
10
+
11
+ DEFAULT_MAX_INPUT_CHARS = 120_000
12
+
13
+
14
+ class StructuredOutputMode(StrEnum):
15
+ """Structured output enforcement mode for OpenAI-compatible Chat Completions."""
16
+
17
+ STRICT_WITH_FALLBACK = "strict_with_fallback"
18
+ STRICT = "strict"
19
+ JSON_OBJECT = "json_object"
20
+
21
+
22
+ class InputBudgetOverflowPolicy(StrEnum):
23
+ """Action taken when prepared document text exceeds InputBudgetPolicy.max_input_chars."""
24
+
25
+ REJECT = "reject"
26
+ TRUNCATE = "truncate"
27
+
28
+
29
+ def parse_structured_output_mode(value: StructuredOutputMode | str) -> StructuredOutputMode:
30
+ """Validate and normalize a StructuredOutputMode enum or string."""
31
+ if isinstance(value, StructuredOutputMode):
32
+ return value
33
+ if isinstance(value, str):
34
+ try:
35
+ return StructuredOutputMode(value.strip().lower())
36
+ except ValueError as exc:
37
+ valid = ", ".join(m.value for m in StructuredOutputMode)
38
+ raise ConfigurationError(
39
+ f"Invalid structured_output_mode {value!r}; expected one of: {valid}."
40
+ ) from exc
41
+ raise ConfigurationError(
42
+ "structured_output_mode must be a StructuredOutputMode or valid string."
43
+ )
44
+
45
+
46
+ def parse_overflow_policy(value: InputBudgetOverflowPolicy | str) -> InputBudgetOverflowPolicy:
47
+ """Validate and normalize an InputBudgetOverflowPolicy enum or string."""
48
+ if isinstance(value, InputBudgetOverflowPolicy):
49
+ return value
50
+ if isinstance(value, str):
51
+ try:
52
+ return InputBudgetOverflowPolicy(value.strip().lower())
53
+ except ValueError as exc:
54
+ valid = ", ".join(p.value for p in InputBudgetOverflowPolicy)
55
+ raise ConfigurationError(
56
+ f"Invalid overflow_policy {value!r}; expected one of: {valid}."
57
+ ) from exc
58
+ raise ConfigurationError(
59
+ "overflow_policy must be an InputBudgetOverflowPolicy or valid string."
60
+ )
61
+
62
+
63
+ def parse_max_input_chars_arg(raw: str) -> int | None:
64
+ """Parse CLI --max-input-chars accepting a positive int or case-insensitive 'none'."""
65
+ cleaned = raw.strip()
66
+ if cleaned.lower() == "none":
67
+ return None
68
+ try:
69
+ parsed = int(cleaned)
70
+ except ValueError as exc:
71
+ raise ConfigurationError(
72
+ f"Invalid --max-input-chars value {raw!r}; expected a positive integer or 'none'."
73
+ ) from exc
74
+ if parsed <= 0:
75
+ raise ConfigurationError("InputBudgetPolicy.max_input_chars must be a positive integer.")
76
+ return parsed
77
+
78
+
79
+ @dataclass(frozen=True, slots=True)
80
+ class RetryPolicy:
81
+ """Bounded retry configuration for transient provider and output failures."""
82
+
83
+ max_retries: int = 2
84
+ initial_backoff_seconds: float = 0.5
85
+ max_backoff_seconds: float = 8.0
86
+ jitter: bool = True
87
+ retry_on_malformed_output: bool = True
88
+ max_total_seconds: float | None = None
89
+
90
+ def __post_init__(self) -> None:
91
+ if isinstance(self.max_retries, bool) or not isinstance(self.max_retries, int):
92
+ raise ConfigurationError("RetryPolicy.max_retries must be an integer >= 0.")
93
+ if self.max_retries < 0:
94
+ raise ConfigurationError("RetryPolicy.max_retries must be an integer >= 0.")
95
+
96
+ if (
97
+ isinstance(self.initial_backoff_seconds, bool)
98
+ or not isinstance(self.initial_backoff_seconds, (int, float))
99
+ or not math.isfinite(self.initial_backoff_seconds)
100
+ or self.initial_backoff_seconds < 0.0
101
+ ):
102
+ raise ConfigurationError(
103
+ "RetryPolicy.initial_backoff_seconds must be a finite number >= 0.0."
104
+ )
105
+ object.__setattr__(self, "initial_backoff_seconds", float(self.initial_backoff_seconds))
106
+
107
+ if (
108
+ isinstance(self.max_backoff_seconds, bool)
109
+ or not isinstance(self.max_backoff_seconds, (int, float))
110
+ or not math.isfinite(self.max_backoff_seconds)
111
+ or self.max_backoff_seconds < self.initial_backoff_seconds
112
+ ):
113
+ raise ConfigurationError(
114
+ "RetryPolicy.max_backoff_seconds must be a finite number "
115
+ ">= initial_backoff_seconds."
116
+ )
117
+ object.__setattr__(self, "max_backoff_seconds", float(self.max_backoff_seconds))
118
+
119
+ if not isinstance(self.jitter, bool):
120
+ raise ConfigurationError("RetryPolicy.jitter must be a boolean.")
121
+ if not isinstance(self.retry_on_malformed_output, bool):
122
+ raise ConfigurationError("RetryPolicy.retry_on_malformed_output must be a boolean.")
123
+
124
+ if self.max_total_seconds is not None:
125
+ if (
126
+ isinstance(self.max_total_seconds, bool)
127
+ or not isinstance(self.max_total_seconds, (int, float))
128
+ or not math.isfinite(self.max_total_seconds)
129
+ or self.max_total_seconds <= 0.0
130
+ ):
131
+ raise ConfigurationError(
132
+ "RetryPolicy.max_total_seconds must be a finite number > 0.0 or None."
133
+ )
134
+ object.__setattr__(self, "max_total_seconds", float(self.max_total_seconds))
135
+
136
+
137
+ @dataclass(frozen=True, slots=True)
138
+ class InputBudgetPolicy:
139
+ """Configurable character-based input budget and overflow handling policy."""
140
+
141
+ max_input_chars: int | None = DEFAULT_MAX_INPUT_CHARS
142
+ overflow_policy: InputBudgetOverflowPolicy = InputBudgetOverflowPolicy.REJECT
143
+
144
+ def __post_init__(self) -> None:
145
+ if self.max_input_chars is not None and (
146
+ isinstance(self.max_input_chars, bool)
147
+ or not isinstance(self.max_input_chars, int)
148
+ or self.max_input_chars <= 0
149
+ ):
150
+ raise ConfigurationError(
151
+ "InputBudgetPolicy.max_input_chars must be a positive integer or None."
152
+ )
153
+ normalized = parse_overflow_policy(self.overflow_policy)
154
+ object.__setattr__(self, "overflow_policy", normalized)