https://www.boundaryml.com logo
Is there a way to make b.stream.Function(...) emit...
# help
s
Hi all — I have a quick question regarding streaming behavior. I’m currently integrating BAML into a LangChain-compatible chat model (ChatBaml).
Copy code
python
async def _astream()
  ...
  stream = self.b.stream.ChooseTool(baml_state, {"tb": tb}) # tb for @@dynamic usage in baml_src/
  async for partial in stream:
    ...
Right now partial.model_dump() is accumulated, e.g.:
Copy code
- 1st emit: {"selected_tool": None}
- 2nd emit: {"selected_tool": {"name": None}}
- 3rd emit: {"selected_tool": {"name": "rep"}}
- 4th emit: {"selected_tool": {"name": "reply_to_user"}}
For LangChain streaming, I’d prefer delta-style updates (only the changed field/value per emit). Is there a configuration to make partial emit delta-style updates instead of accumulated state?
For additional context, the image shows that all LangChain tools are assigned into a BAML DynamicSchema. If we can't make
b.stream.Function()
emit delta-style, is there a function similar to tb.DynamicSchema.model_dump_with_all_optional() that can return an initial “partial” dictionary with all fields set to optional/None? This would allow me to handle the delta-extraction logic on my side more easily. https://cdn.discordapp.com/attachments/1466351374764871762/1466365633418625105/image.png?ex=697c7aea&is=697b296a&hm=c76c8db8193ee23318559d2eb149955e4aceb31b72376f8fcd366eb11eb1e734&
v
Hmm, could you indicate how you would want delta structured values to be represented?
Like in the example you shared above
We also have a on_tick option that gives you access to the underlying method!
s
In my case, since the
DynamicSchema
is already defined using
TypeBuilder
, I was expecting the delta to be a fully shaped dictionary with all fields present but set to None, for example:
Copy code
python
{
    selected_tool: {
        name: None, # could be a delta or accumulated value; mainly need the full schema shape
        arguments: {
            a: None,
            b: None,
        },
    } or {
        name: None,
        arguments: {
            x: None,
            y: None,
        },
    } or {
        name: None,
        arguments: {
            role: "assistant", # constant
            content: None,
        },
    },
}
instead of:
Copy code
python
{"selected_tool": None}
This would make it easier for me to compute delta-style updates on the LangChain side. That said, please feel free to ignore this if it doesn’t align with the intended design. Also, thanks for pointing out on_tick — that’s new to me, and I’ll take a look at the documentation.
It turns out this is a bit more complicated than I initially expected — since the schema uses or, the selected branch isn’t known upfront. For now, I’m going to handle streaming only once I know which tool will be selected. Thanks a lot for the help and discussion!
2 Views