https://www.boundaryml.com logo
🟑 How to add few-shot examples in BAML and improv...
# questions
r
How do I add few-shot examples in BAML? I need formatting help, can it provide that? Anything I do to alter my prompt, including a Gemini suggested prompt alteration kills performance from 80% ID retention to 25%... I can't tune it myself.
a
Can you provide an example of what your prompt is in Baml and how you want it to be rendered? As far as formatting goes, prompts are Jinja templates you can format them however you want, the only thing that's more "built-in" is the
ctx.output_format
but even for that you have [some control](https://docs.boundaryml.com/ref/prompt-syntax/ctx-output-format)
r
You format data for me to make the whole thing work. Do you also do this for few shot?
Hmmm I guess it’s just another variable?
a
Show me an example of a prompt so I can give you a Baml example. For simple categorization, you can do this:
Copy code
typescript
enum Category {
    Positive
    Negative
}

function Categorize(comment: string) -> Category {
    client GPT5
    prompt #"
        Given these examples:

        The product is great!: Positive
        I don't like this product: Negative

            categorize the following comment. {{ ctx.output_format }}

        {{ comment }}
    "#
r
No, they need to be in JSON format.
a
Instead of strings or enums then it's classes
Using
{{ variable }}
formats it as JSON
r
Okay, yeah that is what I was thinking... I will try it. Maybe this will make it work.
@Antonio Sarosi (BAML πŸ‘) @Vaibhav (BAML πŸ‘) how am I doing here?
Copy code
typescript
class CompanyList {
   block_key string? @description("The blocking key used to merge records.")
   block_key_type string? @description("The blocking strategy this block key comes from.")
   block_size int? @description("The number of records merged by entity resolution.")   
   companies Company[]
}

class MergedCompany {
   id int? @description("Unique among all objects. Add to source_ids when merging.")
   source_ids int[]? @description("List of source Company.ids that were merged to create this Company.")
}

class FewShotMergedCompanies {
    companies MergedCompany[]
}

function MultiEntityResolution(company_list: CompanyList, few_shot_examples: FewShotMergedCompanies) -> CompanyList {
  // see clients.baml
  client Gemini20Flash

  // The prompt uses Jinja syntax. Change the models or this text and watch the prompt preview change!
  prompt #"
    Examine the following set of companies and determine... <lots of instructions>...
    ...

    Given these examples:

    {{ few_shot_examples }}

    Return a merged set of companies with all combined attributes from the source companies filled out.

    {# special macro to print the output instructions. #}
    {{ ctx.output_format(hoist_classes=["Company", "Ticker", "CompanyList"]) }}

    {{ _.role("user") }}

    Company List: {{ company_list }}
}
cc @prrao
v
that will work!
r
This schema is going to be awful though... is there a simpler way to do this?
Copy code
python
class MergeCompany {
   id int? @description("Unique among all objects. Add to source_ids when merging.")
   source_ids int[]? @description("List of source Company.ids that were merged to create this Company.")
}

class FewShotExample {
    company MergeCompany
    merged_company MergeCompany
}

class FewShotMergedCompanies {
    merge_companies FewShotExample[]
}
I can't imagine that will perform well in JSON schema / form?
Can I just declare an array of
FewShotExample
classes outside a class? How?
v
you can just pass in a
FewShotExample[]
directly as well if thats what you meant
r
I need more than one.
I need 1-n
v
sorry, could you write the code you wish you could write?
r
Here is my prompt:
Copy code
...
  prompt #"
    Important stuff...

    Given these examples:

    {{ few_shot_company_merges }}
    
    ...

    Return a merged set of companies with all combined attributes from the source companies filled out.

    {# special macro to print the output instructions. #}
    {{ ctx.output_format(hoist_classes=["Company", "Ticker", "CompanyList"]) }}

    {{ _.role("user") }}

    Company List: {{ company_list }}
  "#
I just want to get data in
{{ few_shot_company_merges }}
that is a subset of my big fat
Company
class with just two fields:
id
and
source_ids
, an integer and a list of integers. Here is my 2-field company class,
MergeCompany
in BAML land:
Copy code
class MergeCompany {
   id int? @description("Unique among all objects...")
   source_ids int[]? @description("List of foo...")
}
I need to get a list of those in JSON... in whatever format will make my prompt the most accurate.
v
i see subset problem
there really isn't a way to do that trivially as of now
r
You can't show a list of objects as few shot objects?
v
you can write a custom template_string for printing out that type
then use that for the fewshot example
r
I don't need you to solve the tracking problem, just I need to know what format to use for few-shot. This a core BAML area, right? Formats?
If I was in an object I would think it would just be:
Copy code
merge_companies: MergeCompany[]
But you guys know the best format, not me. That is your entire focus.
So what is that format?
And how do I substitute it in?
Anything I try myself takes the performance from like 80% to 12.5%. No joke. Or 25%. I need help.
v
(on mobile, let me read this when i'm back at my desk in ~20-30 mins!)
r
Ok, thank you.
Here is the only way I have figured out to do it... but I imagine the LLM will get confused by all the nesting:
Copy code
typescript
class CompanyList {
   @@description("Retain all Company.id values in source_ids when merging.")
   block_key string? @description("The blocking key used to merge records.")
   block_key_type string? @description("The blocking strategy this block key comes from.")
   block_size int? @description("The number of records merged by entity resolution.")   
   companies Company[]
}

class MergeCompany {
   @@description("A simple Company object to track merges of Company records.")
   id int? @description("Unique among all objects. Add to source_ids when merging.")
   source_ids int[]? @description("List of source Company.ids that were merged to create this Company.")
}

class FewShotExample {
    companies MergeCompany[] @description("Input companies to be merged.")
    merged_company MergeCompany @description("The resulting merged company, with all the input companies' Company.id values in source_ids.")
}

class MergeCompanyExamples {
    @@description("A collection of few-shot examples for merging companies.")
    merge_companies FewShotExample[]
}

function FewShotMultiEntityResolution(company_list: CompanyList, few_shot_company_merges: MergeCompanyExamples) -> CompanyList {
  // see clients.baml
  client Gemini20Flash
  
  prompt #"
    Given these examples of merging companies:

    {{ few_shot_company_merges }}

    Examine the following set of companies and determine which ones are the same entity...
    ...

    Return a merged set of companies with all combined attributes from the source companies filled out.

    {# special macro to print the output instructions. #}
    {{ ctx.output_format(hoist_classes=["Company", "Ticker", "CompanyList"]) }}

    {{ _.role("user") }}

    Company List: {{ company_list }}
  "#
}
I am naming them better at least, as I iterate in this pastie. The names are at least meaningful to the LLM now.
I am giving away cutting edge methods of entity resolution...
Quinctilius Varus, give me my IP back!
I am actually proud of that, that is a reasonable attempt from someone that doesn't know jack about optimizing few-shot.
@Vaibhav (BAML πŸ‘) okay man, I iterated over and over and that is the very best work I can do on this on my own. Would that make sense to an LLM merging companies and tracking
id
fields in
source_ids
field? I lose 30% of them with blocks of 30 at the moment. I need 100%.
v
I would do something like this:
Copy code
template_string FewShotCompanies(example: MergeCompanyExamples) #"
  For example:
  {% for c in example.companies %}
  {{ c }}
  {% endfor %}
  
  Would merge into:
  {{ example.merged_company }} 
"#
then you would use it via:
{{ FewShotCompanies(few_shot_company_merges) }}
i think that will be be better
less nesting
r
Thanks, that is exactly what I needed. So I do need the classes, I just render differently.
I got few-shot working... check out this insane structure πŸ˜„
Copy code
python
def company_dicts_to_baml(
    merge_company_example_set_dict: dict[
        str,  # merge_companies
        list[  # MergeCompanyExampleSet.merge_companies
            dict[
                str,  # companies / merged_company
                Union[
                    list[dict[str, Union[int, list[int]]]],  # MergeCompany
                    dict[str, Union[int, list[int]]],  # MergeCompany
                ],  # This is also to format the line I hope
            ]  # This is also to format the line I hope
        ],  # This is to format the line I hope
    ],
) -> MergeCompanyExampleSet:
I have no idea if it helps yet... will have to play with different sets of few-shot examples.
Hell yes! Up to 86% retention πŸ™‚ It's a win. That's with 1-shot.
Copy code
2025-09-15 00:49:14,382 - abzu.spark.er_eval - INFO - ENTITY RESOLUTION EVALUATION SUMMARY - ITERATION 1
2025-09-15 00:49:14,382 - abzu.spark.er_eval - INFO - ============================================================
2025-09-15 00:49:14,382 - abzu.spark.er_eval - INFO - Original raw companies (iteration 0): 13,641 total, 13,641 unique
2025-09-15 00:49:14,382 - abzu.spark.er_eval - INFO - Current iteration (1) resolved: 4,731 total, 4,718 unique
2025-09-15 00:49:14,383 - abzu.spark.er_eval - INFO - 
2025-09-15 00:49:14,383 - abzu.spark.er_eval - INFO - DATA REDUCTION:
2025-09-15 00:49:14,383 - abzu.spark.er_eval - INFO -   From original: 8,923 companies (65.41%)
2025-09-15 00:49:14,383 - abzu.spark.er_eval - INFO - 
2025-09-15 00:49:14,383 - abzu.spark.er_eval - INFO - UUID VERIFICATION (should all be 0%):
2025-09-15 00:49:14,383 - abzu.spark.er_eval - INFO -   Overlap with original: 74 UUIDs (0.54%)
2025-09-15 00:49:14,383 - abzu.spark.er_eval - INFO - 
2025-09-15 00:49:14,383 - abzu.spark.er_eval - INFO - SOURCE UUID COVERAGE:
2025-09-15 00:49:14,383 - abzu.spark.er_eval - INFO -   Original companies tracked: 11,855/13,641 (86.91%)
2025-09-15 00:49:14,383 - abzu.spark.er_eval - INFO -   Total unique source_uuids: 11,855
2025-09-15 00:49:14,383 - abzu.spark.er_eval - INFO - 
2025-09-15 00:49:14,383 - abzu.spark.er_eval - INFO - SOURCE UUID VALIDATION:
2025-09-15 00:49:14,383 - abzu.spark.er_eval - INFO -   Valid references: 15,521/15,521 (100.00%)
2025-09-15 00:49:14,383 - abzu.spark.er_eval - INFO -   Invalid references: 0/15,521 (0.00%)
2025-09-15 00:49:14,383 - abzu.spark.er_eval - INFO - ============================================================
Wow it works WELL.
High five @Vaibhav (BAML πŸ‘) !
p
Am I reading that log correctly in that the number of valid references went up from 86% to 100% with fewshot examples?
And the model is Gemini 2.5 Pro?
If so, goddamn, 2.5 pro is such a good model
v
It’s all context πŸ™‚
r
Where do you see 2.5 Pro? Should be 2.0 Flash. No, it went from tracking 71% of UUIDs through an iteration to 86%. And that’s with some random single example. I can get it much better.
I can’t afford 2.5 Pro, this gets expensive. I keep hitting 4M token per minute limits slamming block / match / eval loops over and over.
p
Copy code
2025-09-15 00:49:14,383 - abzu.spark.er_eval - INFO - SOURCE UUID VALIDATION:
2025-09-15 00:49:14,383 - abzu.spark.er_eval - INFO -   Valid references: 15,521/15,521 (100.00%)
2025-09-15 00:49:14,383 - abzu.spark.er_eval - INFO -   Invalid references: 0/15,521 (0.00%)
What does this mean? 100%?
And where is the 86%?
And how does one make up for the remaining 14% 😬 ..
2.5 pro is insane.. And slow too.. Gave up on using it..
I think we have to wait a year longer for models of that calibre to become mainstream
r
The metrics for UUID tracking on the first round of `abzu process er stage --iteration 1 -b 30`: - Unique companies input: 13,641 - Unique companies output: 4,678 - Merged comanies: 8,963 - Reduction percentage: 65.71% - UUID tracking throughput: 86.88% - Lost UUIDs: 13.12% # This is a viable loss level that I can fix - first pass of 1-shot
Those are the metrics and their meaning.
Qualitatively the difference post 1-shot is huge. Check out the first iteration blocks (my baseline blocker for companies uses acronyms + first-word and combines them):
Copy code
sql
+----------+----------+
|block_key |block_size|
+----------+----------+
|NVIDIA    |869       |
|GOOGLE    |310       |
|INTEL     |275       |
|MICROSOFT |252       |
|AMD       |220       |
|META      |192       |
|AMAZON    |183       |
|OPENAI    |163       |
|TSMC      |139       |
|APPLE     |136       |
|SAMSUNG   |119       |
|OKLO      |93        |
|AWS       |84        |
|BROADCOM  |82        |
|TESLA     |80        |
|CLOUDFLARE|69        |
|MP        |67        |
|SK        |65        |
|QUALCOMM  |64        |
|ORACLE    |63        |
+----------+----------+
Check out the largest blocks for iteration 2, based on the merges on the round 1 blocks πŸ˜„ This is why I know it really fucking works. It is obvious major merging of the top companies occured. In the second round they will all get resolved.
Copy code
sql
+---------+----------+
|block_key|block_size|
+---------+----------+
|THE      |46        |
|GOOGLE   |40        |
|US       |37        |
|NVIDIA   |34        |
|AMAZON   |30        |
|SAMSUNG  |27        |
|UC       |26        |
|SK       |26        |
|ST       |24        |
|DC       |22        |
|TM       |22        |
|MICROSOFT|19        |
|AR       |19        |
|PI       |18        |
|LG       |18        |
|PT       |17        |
|INTEL    |16        |
|SI       |16        |
|KT       |15        |
|AMD      |15        |
+---------+----------+
2 Views