Draw the field boundary before choosing a model
Divide fields into immutable input, deterministic enrichment and inferred enrichment. In the example, artifact_id and digest are immutable. format_family comes from an allowlisted mapping. suggested_category and summary are inferred. A model response is permitted to populate only the final two fields. Do not merge arbitrary JSON returned by a model into the original event dictionary.
Assign each field an explicit consumer. Search may use the summary. A user interface may display the suggested category with its inferred status. A deployment decision should continue using signed metadata and deterministic validation. Otherwise a harmless-looking descriptive field can become an indirect control input when a later component starts trusting it as an authoritative classification.
Schema validation is necessary but incomplete. A short string fits a schema even when it is false. An allowed enum fits even when the evidence points elsewhere. Separate structural checks from evidence checks and from policy checks. Preserve the result of each check in the enrichment record so a consumer can distinguish malformed output, unsupported inference and a valid but unused suggestion.
An original synthetic fixture
Use three artifacts: one with a clear diagnostic about a missing parser, one with ambiguous text, and one whose model response attempts to replace the digest. The scripted model returns predetermined dictionaries. This offline fixture tests boundary handling and fallback behavior. It does not evaluate a model's accuracy or claim that any external provider was contacted.
For the clear case, accept a category from the allowed set and a summary whose evidence identifiers point to the input diagnostic. For the ambiguous case, retain unknown and a deterministic summary such as Artifact requires inspection. For the attempted overwrite, reject the entire inferred object and retain the original artifact. Do not salvage a maliciously structured response by quietly ignoring only its most obvious unauthorized field.
# Author: Sandeep Belgavi. Deterministic merge boundary.
ALLOWED = {"summary", "suggested_category", "evidence_ids"}
CATEGORIES = {"parser", "configuration", "unknown"}
def apply_enrichment(event, proposal):
if set(proposal) != ALLOWED:
return {**event, "enrichment": {"status": "rejected_fields"}}
if proposal["suggested_category"] not in CATEGORIES:
return {**event, "enrichment": {"status": "rejected_category"}}
return {**event, "enrichment": {"status": "candidate", **proposal}}
original = {"artifact_id": "A-17", "digest": "synthetic-digest"}
bad = {"digest": "replacement", "summary": "looks fine"}
assert apply_enrichment(original, bad)["digest"] == "synthetic-digest"This small function illustrates field isolation only. A complete adapter must also validate value types, summary length and evidence references before returning an accepted status. The distinction matters because copying a short teaching snippet into a service does not supply the missing checks. Treat candidate as an intermediate state, never a synonym for verified truth.
Fallback is a product decision
Choose whether enrichment is optional before adding retries. If the event must remain searchable even when the model is unavailable, emit the deterministic envelope with enrichment.status=unavailable. If a consumer genuinely requires an inferred field, route the event to an explicit pending state. Do not substitute a guessed category merely to satisfy a non-null database column.
Timeouts should produce a different status from invalid output. Repeating an unavailable request may help after a transient failure. Repeating a structurally invalid result indefinitely can amplify cost without improving the contract. Use a bounded retry policy with a total deadline. Record attempts and model configuration, but avoid storing private prompts or unrestricted input bodies in routine logs.
Fallback text also deserves tests. It must not imply that inspection passed, that an artifact is safe, or that a human reviewed the result. Description unavailable is honest. No problems found is a factual claim that requires evidence. This distinction becomes particularly important when users scan summaries rather than opening the underlying diagnostic record.
Preserve replay and provenance
An enrichment record should identify the input digest, adapter version, schema version and model configuration used for the attempt. Store the accepted output separately from the raw provider response if retention policy permits retaining the latter. When replaying a downstream consumer, use the recorded enrichment. Calling the model again creates a new derivation and should receive a new revision identifier.
Caching should follow the same logic. A cache key based only on artifact ID becomes unsafe when diagnostic text or the enrichment schema changes. Include a canonical digest of the relevant input and the policy version. Do not include mutable display timestamps that invalidate every entry without changing its meaning. Document exactly which fields participate in the digest and test that contract.
If a source record changes, decide whether prior enrichment remains visible as history or is invalidated immediately. Both can be legitimate, but mixing them is confusing. A consumer that reads a new artifact with an old summary needs an explicit stale marker. An agent cannot infer freshness reliably from a fluent description, so freshness needs explicit machine-readable metadata.
Failure analysis at the consumer boundary
The most subtle failure is semantic promotion. A downstream service begins routing work by suggested_category because the field is convenient. The inference has become a control decision without a corresponding review of evidence, failure behavior or rollback. Prevent that with separate schema namespaces, documented consumer contracts and tests that reject use of inferred fields in protected decisions.
Another failure is a retry overwriting a previously accepted result. Use compare-and-set on the enrichment revision or append immutable revisions. A slower earlier request must not replace a newer accepted output. This is ordinary concurrency control; the presence of a model does not remove the need to order writes. Include the input digest in the conditional update so a result cannot attach to a different artifact revision.
Batch processing adds partial failures. Preserve one result per input identifier and validate that there are no missing, duplicated or invented identifiers. Never zip input and output arrays by position unless the provider contract guarantees and validates that correspondence. An enrichment system should explain which artifact failed without making the rest of the batch disappear or attaching one summary to another artifact.
Migration and verification
First introduce the enrichment envelope with deterministic fields only. Update consumers to tolerate absent inferred values and unknown enum members according to the versioning policy. Then run scripted fixtures through the entire pipeline. Add the model in shadow mode, compare candidate outputs with the expected field boundaries, and enable display use before considering any operational use.
Meaningful tests include an unauthorized key, invalid enum, oversized summary, unknown evidence reference, stale input digest, duplicate batch identifier and late response. Assert preservation of every immutable field. Test the fallback with the model adapter raising a timeout before producing output. These checks establish the service boundary; a separate labeled dataset is needed to assess inference quality.
Use a lookup or deterministic parser when the information already exists in structured form. A model earns its place when interpretation of unstructured diagnostics materially improves discovery. Even then, keep the original event useful on its own. An optional, versioned suggestion is easier to remove, replace and audit than an inferred field that quietly becomes part of the system's identity.