baptiste_gautier_73323
08/21/2025, 3:44 PMstream.get_final_response()
I will put an example in thread, maybe I'm missing something here !baptiste_gautier_73323
08/21/2025, 3:46 PMclass 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 ?"
}
}baptiste_gautier_73323
08/21/2025, 3:47 PMasync 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, collectorbaptiste_gautier_73323
08/21/2025, 3:48 PMasync 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, collectorbaptiste_gautier_73323
08/21/2025, 3:49 PM@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}")baptiste_gautier_73323
08/21/2025, 3:52 PMCollector 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) 😄