https://www.boundaryml.com logo
🟡 Issue with token persistence in stream collecto...
# questions
b
Hello guys, hope you're doing well ! I encounter an issue that I didn't find the fix on the documentation, so I'm trying here 🙂 It seems that usage is not properly persisted in the collector when streaming answer, input and output tokens are set to None when trying to access them after the stream is completed (i.e after waiting for
stream.get_final_response()
I will put an example in thread, maybe I'm missing something here !
Here is the BAML code:
Copy code
class QueryAnswer {
    elements string[] @description(#"
        A list of elements that will be used to generate the final answer.
        Each element should be a string that represents a thought or a piece of information.
    "#)
    final_answer string @description(#"
        The final answer that is generated based on the elements.
        This should be a concise summary or conclusion derived from the elements.
    "#)
}


function QueryAnswering(query: string) -> QueryAnswer {
    client FastLLM // My LLM client

    prompt #"
    {{ _.role("system") }}
    You are an expert in conducting deep reasoning in order to solve user queries.
    You will be provided a query, answer it by generating a list of elements that will be used to derive the final answer. 

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

    {{ ctx.output_format }}
    "#
}


test TestQueryAnswering {
    functions [QueryAnswering]

    args {
        query "Why is my baml collector not retrieving usage data when the answer is streamed ?"
    }
}
The test works well, in python I've setup two functions: 1 to do the generation in streaming:
Copy code
async def streamed_generation():
    lm_client = (
        setup_test_client()
    )  # an object that contains the Baml client and registry

    query = "Why is my baml collector not retrieving usage data when the answer is streamed ?"

    collector = Collector()

    stream = lm_client.baml_client.stream.QueryAnswering(
        query=query,
        baml_options=BamlCallOptions(
            client_registry=lm_client.registry,
            collector=collector,
        ),
    )

    async for partial in stream:
        yield partial

    final = await stream.get_final_response()

    print(f"Collector meta: {collector.last.usage}")

    yield final, collector
And another one to do the generation in a non streamed way:
Copy code
async def non_streamed_generation():
    lm_client = setup_test_client()

    query = "Why is my baml collector not retrieving usage data when the answer is streamed ?"

    collector = Collector()

    answer = await lm_client.baml_client.QueryAnswering(
        query=query,
        baml_options=BamlCallOptions(
            client_registry=lm_client.registry,
            collector=collector,
        ),
    )

    print(f"Collector meta: {collector.last.usage}")

    return answer, collector
Finally I run both and check for collector usage status of both:
Copy code
@pytest.mark.asyncio
async def test_streamed_generation():
    streaming_generator = streamed_generation()

    results = []

    async for result in streaming_generator:
        results.append(result)

    _, streamed_collector = results[-1]

    _, non_streamed_collector = await non_streamed_generation()

    print(f"Streamed collector last usage: {streamed_collector.last.usage}")
    print(f"Non streamed collector last usage: {non_streamed_collector.last.usage}")
Running this leads to this answer:
Copy code
Collector meta: Usage(input_tokens=None, output_tokens=None) # The one called in the streamed_generation
Collector meta: Usage(input_tokens=149, output_tokens=188) # The one called in the non_streamed_generation
Streamed collector last usage: Usage(input_tokens=None, output_tokens=None) 
Non streamed collector last usage: Usage(input_tokens=149, output_tokens=188)
# the two called in the final prints statements
Answer is properly computed, and the generation is done. But I can't figure out why my streamed collector has empty usage 🤔 If someone can help me with this, I'd be very happy ! Thanks for your reading (and your help) 😄