0Pricing
Claude Architect · Lektion

Enums mit „other“ für Erweiterbarkeit

Fügen Sie einen Wert „other“ sowie ein Detailfeld hinzu

Enums mit „other“ für Erweiterbarkeit ist eine kostenlose Claude Architect-Lektion auf CoddyKit. Dies ist Lektion 4 von 4. Du kannst die komplette Lektion unten kostenlos lesen – dann übst du sie direkt im Browser mit einem integrierten Code-Editor und einem KI-Tutor rund um die Uhr. Sie ist Teil des Claude Architect-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der Claude Architect-Kurs umfasst insgesamt 4 Lektionen.

Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.

The Problem with a Closed Enum

Enums are the backbone of clean structured output. When you constrain a field to a fixed set of values — like category being one of billing, technical, or account — the model can't drift into free-form strings, and downstream code can branch with confidence.

But a closed enum has a hidden failure mode: the real world produces inputs your list never anticipated. When a ticket doesn't fit any allowed value, the model is forced to pick the least wrong option. That's a silent misclassification, and it corrupts everything built on top of it.

The Extensibility Pattern: 'other' + detail

The architect-grade fix is the extensibility pattern: add an "other" value to the enum, plus a free-text detail field the model fills in when it selects other.

This gives you two wins at once:

  • The model has an honest escape hatch instead of a forced bad fit.
  • You capture what the unexpected case actually was — raw signal you can mine to grow the enum later.

It turns the gap in your taxonomy into a logged, inspectable data point instead of a corrupted record.

A Schema with 'other'

Here is a classification tool for a support agent. Notice the enum includes "other", and there's a companion category_detail string for the open case.

Because structured output via tool_use + JSON Schema enforces the enum, the model literally cannot return a value outside the list — including the deliberate other safety valve.

classify_ticket = {
    "name": "classify_ticket",
    "description": "Classify a support ticket into one category.",
    "input_schema": {
        "type": "object",
        "properties": {
            "category": {
                "type": "string",
                "enum": ["billing", "technical", "account", "other"],
                "description": "Use 'other' ONLY when none of the named categories fit."
            },
            "category_detail": {
                "type": "string",
                "description": "Required when category is 'other': a short phrase naming the real category."
            }
        },
        "required": ["category"]
    }
}

Why detail Must NOT Be Required

The most important schema decision here: mark category_detail as optional, never required.

A core structured-output rule is to require a field only if it is always present. The detail field is present only when category is other — it is absent for billing, technical, and account tickets.

If you force it into required, the model will fabricate a detail string for cases that don't need one, just to satisfy the schema. You'd be manufacturing hallucinations to enforce a rule you didn't actually need.

Conditional Requirement Lives in the Prompt

So how do you make sure category_detail is filled when the model picks other? The dependency is conditional, so it belongs in the field description and the system prompt — not in the schema's static required array.

Be explicit about the rule. Explicit criteria beat vague instructions every time.

system = (
    "Classify each ticket using the classify_ticket tool.\n"
    "Pick the most specific named category that fits.\n"
    "Choose 'other' ONLY when no named category applies. "
    "When you choose 'other', you MUST also provide category_detail "
    "as a short noun phrase naming the true category "
    "(e.g. 'partnership inquiry', 'legal request')."
)

Forcing Structured Output with tool_choice

To guarantee the model returns the classification as structured data rather than prose, force the tool call. Setting tool_choice to {"type": "tool", "name": "classify_ticket"} compels Claude to emit exactly that tool's input, schema-validated.

If you had several extraction tools and just wanted to guarantee some structured call, tool_choice: "any" would do; "auto" lets the model choose between text and a tool. For a single forced schema, name the tool explicitly.

resp = client.messages.create(
    model="claude-sonnet-4-5",
    max_tokens=512,
    system=system,
    tools=[classify_ticket],
    tool_choice={"type": "tool", "name": "classify_ticket"},
    messages=[{"role": "user", "content": ticket_text}],
)

Reading the Result Safely

On the consuming side, branch on the enum. The other bucket is where you read category_detail — and where you should expect it to sometimes be missing, because it's optional.

Defensive access (a default of empty string) keeps your code robust against a legitimately absent field, which is exactly the behavior you designed for.

block = next(b for b in resp.content if b.type == "tool_use")
result = block.input
category = result["category"]

if category == "other":
    detail = result.get("category_detail", "")
    log_uncategorized(ticket_text, detail)  # capture for later enum growth
else:
    route_to_queue(category)

Mining 'other' to Grow the Enum

The detail field isn't just a fallback — it's a feedback loop. Every other record carries a human-readable label for a case your taxonomy missed.

Periodically aggregate those details. When a cluster like "partnership inquiry" shows up 40 times a week, you've found your next first-class enum value. You promote it from free text to a named category, and your classifier gets sharper — driven by real data, not guesswork.

Pairs Naturally with Retry-with-Feedback

This pattern composes cleanly with validation. If a result violates your conditional rule — say category is other but category_detail came back empty — that's a structural error, and retry-with-feedback fixes exactly those.

Send the model the original ticket, its own wrong output, and the precise validation error. Crucially, retry helps because the information needed (the real category) is present in the source. Retry would not help if the information were simply absent from the ticket.

if result["category"] == "other" and not result.get("category_detail"):
    feedback = (
        "You returned category='other' but left category_detail empty. "
        "Re-classify and provide a short detail phrase."
    )
    # resend: original ticket + the bad output + this exact error

When NOT to Add 'other'

The pattern is for extensibility under uncertainty — open-world taxonomies like ticket categories, document types, or intent labels where new cases will keep appearing.

Don't bolt other onto a genuinely closed set. A priority field of low / medium / high is exhaustive by definition; an other there just invites the model to dodge a real decision. Reserve the escape hatch for enums that legitimately can't anticipate every value.

The Full Pattern in One View

Put the pieces together and you have a small, robust contract:

  • Enum with named values plus other — the schema enforces the closed set.
  • Optional detail field — never required, so the model won't fabricate it.
  • Prompt + description carry the conditional "fill detail when other" rule.
  • Consumer branches on the enum and logs the detail.
  • Validation/retry catches a missing-but-required-by-rule detail.

It's a clean separation: the schema guarantees shape; the prompt guarantees behavior; the loop captures what you didn't foresee.

Quick Check: Designing the 'other' Field

You're building a ticket-classifier tool whose category enum includes billing, technical, account, and other. You add a category_detail string that should be populated whenever the model picks other. How should you encode the requirement that category_detail be filled for other cases?

Recap: Extensible Enums

Key takeaways:

  • Closed enums silently misclassify inputs they never anticipated; an other value gives the model an honest escape hatch.
  • Pair other with a free-text detail field to capture what the unexpected case actually was.
  • Keep the detail field optional — never require a field that may be absent, or the model will fabricate it.
  • Put the conditional "fill detail when other" rule in the prompt and field description, not the static required array.
  • Mine logged other details to grow the enum with real first-class values over time.
  • Use tool_use + JSON Schema to enforce the enum, and retry-with-feedback for structural violations.

Häufig gestellte Fragen

Ist die Lektion „Enums mit „other“ für Erweiterbarkeit“ kostenlos?

Ja — der vollständige Text von „Enums mit „other“ für Erweiterbarkeit“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des Claude Architect-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der Claude Architect-Kurs umfasst insgesamt 4 Lektionen.

Was lerne ich in „Enums mit „other“ für Erweiterbarkeit“?

Fügen Sie einen Wert „other“ sowie ein Detailfeld hinzu Du übst Claude Architect mit praktischem Code, den du direkt im Browser ausführst, und ein 24/7 KI-Tutor beantwortet deine Fragen während du die Lektion bearbeitest.

Brauche ich Erfahrung, um Claude Architect zu starten?

Keine Vorkenntnisse erforderlich. Claude Architect auf CoddyKit ist für Anfänger bis fortgeschrittene Lernende strukturiert, sodass du hier starten oder von Anfang an beginnen und in deinem eigenen Tempo voranschreiten kannst. Dies ist Lektion 4 von 4.

Wie lange dauert die Lektion „Enums mit „other“ für Erweiterbarkeit“?

Die meisten CoddyKit-Lektionen dauern etwa 5–10 Minuten. Jede ist kompakt und interaktiv, sodass du stetig Fortschritte machst und genau dort weitermachst, wo du aufgehört hast – im Web und in der App.

Kann ich in dieser Claude Architect-Lektion Code schreiben und ausführen?

Ja. Jede Claude Architect-Lektion enthält einen integrierten Code-Editor, sodass du echten Code direkt in deinem Browser schreibst und ausführst und sofort KI-Feedback erhältst — ohne lokale Einrichtung erforderlich.

Alle Lektionen in diesem Kurs

  1. tool_use für garantierte Strukturen
  2. Ein JSON-Schema entwerfen
  3. Erforderliche vs. optionale/nullbare Felder
  4. Enums mit „other“ für Erweiterbarkeit
← Zurück zu Claude Architect