Repository navigation
Closed
Description
To support model agnosticism, the system design originally converted contests into conversation JSON format using this pipeline:
Context --> list of Components --> list of `stdlib.chat.Messages` --> conversation JSON to pass to model endpoint
There were pretty specific contracts at each point in this pipeline, and it was really important that each Message was only content + role + optional image. (That's why there is a special code path for Message in utils.py).
This has a couple of problems:
- we're using
stdlib.chat.Messagesas both a Component and an IR for the pipeline. This special treatment is bad, because it creates a special case treatment forMessage, which leads to bugs (see: Documents dropped in standard chat context path (generate_from_chat_context) #949) - some APIs/models want us to break out images/docs/tools/etc. into top-level fields.
- some APIs/models don't want us to do that same break-out
This means that we need an additional step in backends that extracts stuff from template reprs and puts it into other api args or fields on the conversation json.
Propose fixing this by making the flow look more like:
Context --> formatted Components |--> content --> content with inlined stuff not extractable to api args |
| AND their ->| ^ |--> conversation JSON + api args
|-> Template Reprs. |--> API Args --| |
* docs
* images
* etc.
The pipeline psuedocode looks like:
msgs = list[Component | CBlock | ModelOutputThunk] = context.to_list()
formatted_messages : list[Tuple[RoleName, MessageContents, TemplateRepresentation]] = [Formatter.format(msg) for msg in msgs]
# where RoleName == str, MessageContents == str, TemplateRepresentation is a dict with stuff that we might want to extract.
# this code depends on whether doc handling can be done entirely by the formatter or not.
# ie, whether it's a model-specific thing or a mode l +infernce engine thing.
if model and IE have special docs handling:
api_args.extend(...or wahtever..)
else:
formatted_messages : list[Tuple[RoleName, MessageContents, TemplateRepresentation]] = [Formatter.inline_docs(msg) for msg in msgs]
# and similar for images and other stuff...
Metadata
Metadata
Assignees
Labels
No labels
Activity
nrfulton commentedon Apr 28, 2026
Then we need to add
docstoTemplateRepresentation:nrfulton commentedon Apr 28, 2026
A better way of saying this is that there are 3 processing steps:
The benefit of the rewriting happening in 3 is that it's more a defensive way of programming this (silently dropping a template arg would be a lot harder.
jakelorocco commentedon Apr 28, 2026
The open question is then how do we want to actually handle the documents that we extract. Intrinsics allow specifying a field that determines the outcome: docs_as_message with the transformations.
The equivalent in Mellea would be to pass in a ModelOption that indicates how to handle docs, and we choose a strategy to default to.
nrfulton commentedon Apr 28, 2026
If the model supports a docs kwarg then we pass it in there; else, the formatter needs to specify how the cos get inlined into the template (and throw exception of the template for that component doesn't explain what to do with docs).
For messages, we can support of the docs kwarg by appending? or prepending?