LLMs don't always give you what you expect. Sometimes they need clarification. Sometimes they refuse. BAML lets you handle all outcomes in one function.
One function, multiple typed outcomes. No more checking if response.error or parsing edge cases manually.
class ContactInfo {
name string
email string?
phone string?
}
class SuccessfulExtraction {
data ContactInfo
confidence float @description("0.0 to 1.0")
}
class NeedsMoreInfo {
question string @description("What clarification is needed")
}
class CannotProcess {
reason string
}
function SmartExtract(text: string) -> SuccessfulExtraction | NeedsMoreInfo | CannotProcess {
client "openai/gpt-4o-mini"
prompt #"
Extract contact info from: {{ text }}
If the text is clear, return the data with confidence score.
If you need clarification, ask a specific question.
If you cannot process it, explain why.
{{ ctx.output_format }}
"#
}