https://www.boundaryml.com logo
🟔 Bug Report: Jinja2 `flatten` Operator Missing
# questions
r
I have another bug to report: Jinja2
flatten
operator is missing šŸ™
v
thanks for reporting this! @baml-sam can we make this a quick friday?
r
Thanks, I need to to validate that all output company IDs existed in the input company list.
@Vaibhav (BAML šŸ‘) did you guys get to this?
@baml-sam any idea when this might get fixed?
b
Antonio was looking at this, but double-checking it, I'm not sure what you mean by "flatten is missing" - it doesn't appear to exist in the [jinja library we use](https://docs.rs/minijinja/latest/minijinja/filters/index.html#functions), nor does it appear in [jinja itself](https://jinja.palletsprojects.com/en/stable/templates/#filters)
r
I see, sorry, it looks like it was part of ansible.
b
Ahh, gotcha. To be clear, we can definitely add it, btw, just like how we expose stuff through
_.role
and
ctx.output_format
- but we're also working on a whole bunch of other things that will enable us to eventually provide a more powerful standard library, and get rid of things like jinja filters
So I was trying to figure out what we were missing around
flatten
. Thanks for the context!
r
I have a
CompanyList
as input and a
CompanyList
as output in a
MatchingResult
where I do multiple record match + merges. I need to validate the result in the exported Pydantic object by checking that all entity IDs in the output `CompanyLists`'s
Company.source_ids
field came from the input `CompanyList`'s
Company.id
fields. There seems no other way to do this without that operator...
If the source IDs are erroneous, I just want the object to fail validation and throw an exception, probably retry that prompt. I can't trust that result.
@baml-sam is there a way to do that in BAML? I want it n the Pydantic class.
b
Hmm - I'm not sure how
flatten
would have helped with this, but this feels like a thing we need to improve
@assert
for
Because we'd need to somehow expose the
input
args to an assertion
So that is to say that no, I don't think we can do that in BAML right now. We're working on some stuff that will let you do that eventually
r
They are open to assertion, so far as I can tell.
It’s just there aren’t the map / flatten list operations to do the comparison.
Lemme pull the actual code…
Here’s the kind of validation I do that’s similar… one list’s membership in another.
flatten
would help here:
Copy code
@@assert(referential_integrity, {{ 
    this.relationships is not none and
    this.relationships|map(attribute='src_company')|
    select('in', this.companies|map(attribute='id'))|list|length == 
    this.relationships|length and
    this.relationships|map(attribute='dst_company')|
    select('in', this.companies|map(attribute='id'))|list|length == 
    this.relationships|length
  }})
I couldn’t get my LLM to write the function I described without employing
flatten
but maybe it can if I keep at it. Or aghast… I could write it šŸ™‚
b
I cajoled claude into giving me this:
Copy code
{{
    relationships is not none and
    relationships|rejectattr('src_company', 'in', companies|map(attribute='id'))|list|length == 0 and
    relationships|rejectattr('dst_company', 'in', companies|map(attribute='id'))|list|length == 0
}}
Which I think works? Here's what I was testing with in [minijinja](https://mitsuhiko.github.io/minijinja-playground/):
Copy code
{%- set relationships = [
    {"src_company": 1, "dst_company": 2},
    {"src_company": 2, "dst_company": 3},
    {"src_company": 1, "dst_company": 3}
] -%}
{%- set companies = [
    {"id": 1},
    {"id": 2},
    {"id": 3}
] -%}

Every src_company and dst_company exists in companies: {{ 
    relationships is not none and
    relationships|map(attribute='src_company')|
    select('in', companies|map(attribute='id'))|list|length == 
    relationships|length and
    relationships|map(attribute='dst_company')|
    select('in', companies|map(attribute='id'))|list|length == 
    relationships|length
}}

using rejectattr: {{
    relationships is not none and
    relationships|rejectattr('src_company', 'in', companies|map(attribute='id'))|list|length == 0 and
    relationships|rejectattr('dst_company', 'in', companies|map(attribute='id'))|list|length == 0
}}
(I'm realizing we probably need to provide some form of this to users to make testing complicated check/assert expressions easier)
r
The code I posted up top does the job for that task. The task I want flatten for is to validate that members of one list come from another.