jungealexander
09/28/2025, 7:58 PMclass Ranking {
rank int
id int
}
class RankedDocuments {
ranked_documents Ranking[]
}
How should I best go about enforcing that each of the N unique IDs is ranked exactly once?
I could imagine trying to do this a) in BAML, or b) in (Python) code interacting with the BAML client to dynamically adjust prompt/output models, or c) in code interacting with the parsed BAML response to retry the LLM with an informative error message explaining why the produced ranking is invalid. What is the "BAML way" to do this?
I think that I can hardly be the first person with a usecase like this, so pointers to similar examples would be great, too, as I could not find anything.greg (BAML)
09/28/2025, 11:38 PMclass RankedDoc {
doc_prefix string @description("First several words of the input doc")
rank int
}
function Rank(inp: string[]) -> RankedDoc[] {
client SomeClient
prompt #"
Produce a new ranking for each of the input docs in {{ inp }}.
For each output ranking, include the first few words from the
input document.
The order of the output ranks should be the same as the order
of the input docs. The input is a batch. Do not reorder the
documents by rank.
{{ ctx.output_format }}
"#
}greg (BAML)
09/28/2025, 11:43 PMiakashvekariya
09/29/2025, 8:04 PMprrao
09/30/2025, 12:59 AMprrao
09/30/2025, 1:00 AMprrao
09/30/2025, 1:01 AMprrao
09/30/2025, 1:02 AMiakashvekariya
09/30/2025, 4:42 AMjungealexander
09/30/2025, 8:32 AMVaibhav (BAML 🐑)
09/30/2025, 2:43 PMfunction Rank(inp: string[]) -> RankedDoc[] {
client SomeClient
prompt #"
Produce a new ranking for each of the input docs.
For each output ranking, include the first few words from the
input document.
The order of the output ranks should be the same as the order
of the input docs. The input is a batch. Do not reorder the
documents by rank.
{{ ctx.output_format }}
{{ _.role('user') }}
{% for doc in inp %}
{{ loop.index0 }}: {{ doc }}
{% endfor %}
"#
}Vaibhav (BAML 🐑)
09/30/2025, 2:44 PMVaibhav (BAML 🐑)
09/30/2025, 2:44 PMVaibhav (BAML 🐑)
09/30/2025, 2:44 PMclass RankedDoc {
doc_index int
doc_prefix string @description("First several words of the input doc")
rank int
}Vaibhav (BAML 🐑)
09/30/2025, 2:44 PMdoc_index and doc_prefix) before you do rank.Vaibhav (BAML 🐑)
09/30/2025, 2:44 PMVaibhav (BAML 🐑)
09/30/2025, 2:45 PM