Paco Nathan
08/07/2025, 4:33 PMprrao
08/07/2025, 5:47 PMprrao
08/07/2025, 5:48 PMprrao
08/07/2025, 5:49 PMprrao
08/07/2025, 5:50 PMprrao
08/07/2025, 5:50 PMPaco Nathan
08/07/2025, 6:34 PMllama3:latest model via Ollama.
The question given as the input parameter to b.RAG(question, context) almost always gets rewritten. For example:
ic| question: 'how many people were in the study?'
ic| response: Response(question='What is the association between processed red meat and dementia?', answer='According to the study, eating a daily average of one-quarter or more of a serving of processed red meats (roughly two slices of bacon, one and half slices of bologna, or a hot dog), compared to those consuming a minimal amount (less than one-tenth a serving each day), had a 13 percent higher risk of developing dementia.')Paco Nathan
08/07/2025, 6:35 PMPaco Nathan
08/07/2025, 6:38 PMResponse object embedded, and there's generally an answer which is usable. I may just try to trap these exceptions and parse them, but I'd really REALLY like to disable question rewrites.
Overall, having to trap and parse exceptions seems to be defeating the point of using BAML? And I'd really like to use BAML πprrao
08/07/2025, 6:40 PMPaco Nathan
08/07/2025, 6:40 PMcontext looks good. However the question rewriting causes the results to be relatively poor.prrao
08/07/2025, 6:40 PMprrao
08/07/2025, 6:41 PMgemma3:12bPaco Nathan
08/07/2025, 6:41 PMfunction RAG(question: string, context: string) -> Response {
client "openai/gpt-4o-mini"
prompt #"
Answer the question in full sentences using the provided context.
Do not make up an answer. If the information is not provided in the context, say so clearly.
QUESTION: {{ question }}
RELEVANT CONTEXT: {{ context }}
{{ ctx.output_format }}
RESPONSE:
"#
}prrao
08/07/2025, 6:41 PMPaco Nathan
08/07/2025, 6:42 PMprrao
08/07/2025, 6:42 PMPaco Nathan
08/07/2025, 6:43 PMPaco Nathan
08/07/2025, 6:43 PMprrao
08/07/2025, 6:43 PMPaco Nathan
08/07/2025, 6:44 PMgemma downloaded within the hour to compareprrao
08/07/2025, 6:44 PMprrao
08/07/2025, 6:44 PMllama3:latest on my end with ollamaprrao
08/07/2025, 6:44 PMprrao
08/07/2025, 6:44 PMPaco Nathan
08/07/2025, 6:45 PMPaco Nathan
08/07/2025, 6:46 PMprrao
08/07/2025, 6:51 PMprrao
08/07/2025, 6:51 PMprrao
08/07/2025, 6:55 PMrs
class Response {
question string
answer string
}
function RAGTest(question: string, context: string) -> Response {
client OllamaLlama3
prompt #"
Answer the question in full sentences using the provided context.
Do not make up an answer. If the information is not provided in the context, say so clearly.
QUESTION: {{ question }}
RELEVANT CONTEXT: {{ context }}
{{ ctx.output_format }}
RESPONSE:
"#
}
test RAG1 {
functions [RAGTest]
args {
question "How many people were in the study?"
context "According to the study, eating a daily average of one-quarter or more of a serving of processed red meats (roughly two slices of bacon, one and half slices of bologna, or a hot dog), compared to those consuming a minimal amount (less than one-tenth a serving each day), had a 13 percent higher risk of developing dementia."
}
}
test RAG2 {
functions [RAGTest]
args {
question "What is the association between processed red meat and dementia?"
context "According to the study, eating a daily average of one-quarter or more of a serving of processed red meats (roughly two slices of bacon, one and half slices of bologna, or a hot dog), compared to those consuming a minimal amount (less than one-tenth a serving each day), had a 13 percent higher risk of developing dementia."
}
}prrao
08/07/2025, 6:56 PMjson
{
"question": "How many people were in the study?",
"answer": "This information is not provided in the given context.",
}prrao
08/07/2025, 6:56 PMjson
{
"question": "What is the association between processed red meat and dementia?",
"answer": "According to the study, eating a daily average of one-quarter or more of a serving of processed red meats had a 13 percent higher risk of developing dementia compared to those consuming a minimal amount."
}prrao
08/07/2025, 6:56 PMprrao
08/07/2025, 6:59 PMResponse object on the Python side. But from the BAML end, there is no query rewriting happeningPaco Nathan
08/07/2025, 7:00 PMmain branch of https://github.com/DerwenAI/strwythura
if you run demo.py then it generates data.prrao
08/07/2025, 7:00 PMPaco Nathan
08/07/2025, 7:01 PMrag.py workflow this morning, when I ran into all the question rewrites suddenly, and exceptions.
This test code isn't in a state to check in.Paco Nathan
08/07/2025, 7:01 PMrag.py script, that is calling BAML.Paco Nathan
08/07/2025, 7:01 PMb.RAG(question, context)prrao
08/07/2025, 7:02 PMResponse objectPaco Nathan
08/07/2025, 7:02 PMprrao
08/07/2025, 7:02 PMprrao
08/07/2025, 7:02 PMprrao
08/07/2025, 7:03 PMprrao
08/07/2025, 7:03 PMbaml_src/rag.baml with these exact same questions and contextsPaco Nathan
08/07/2025, 7:03 PMprrao
08/07/2025, 7:03 PMprrao
08/07/2025, 7:03 PMprrao
08/07/2025, 7:03 PMPaco Nathan
08/07/2025, 7:04 PMprrao
08/07/2025, 7:04 PMprrao
08/07/2025, 7:04 PMprrao
08/07/2025, 7:07 PMllama3:latest):
client<llm> Llama3Client {
provider "openai-generic"
options {
base_url "http://localhost:11434/v1"
model "llama3:latest"
}
}prrao
08/07/2025, 7:07 PMPaco Nathan
08/07/2025, 7:08 PMPaco Nathan
08/07/2025, 7:09 PMic| question: 'does red meat cause alzheimer's disease?'
ic| response: Response(question='', answer='')prrao
08/07/2025, 7:09 PMprrao
08/07/2025, 7:09 PMllama3:latest as above?prrao
08/07/2025, 7:11 PMprrao
08/07/2025, 7:12 PMrs
test RAG3 {
functions [RAGTest]
args {
question ""
context "According to the study, eating a daily average of one-quarter or more of a serving of processed red meats (roughly two slices of bacon, one and half slices of bologna, or a hot dog), compared to those consuming a minimal amount (less than one-tenth a serving each day), had a 13 percent higher risk of developing dementia."
}
}
json
{
"question": "According to the study, eating a daily average of one-quarter or more of a serving of processed red meats (roughly two slices of bacon, one and half slices of bologna, or a hot dog), compared to those consuming a minimal amount (less than one-tenth a serving each day), had a ...",
"answer": "13 percent higher risk of developing dementia"
}prrao
08/07/2025, 7:12 PMprrao
08/07/2025, 7:12 PMgemma3:12b nowprrao
08/07/2025, 7:13 PMPaco Nathan
08/07/2025, 7:13 PMgemma model to tryprrao
08/07/2025, 7:13 PMPaco Nathan
08/07/2025, 7:14 PMprrao
08/07/2025, 7:14 PMprrao
08/07/2025, 7:14 PMprrao
08/07/2025, 7:14 PMprrao
08/07/2025, 7:15 PMPaco Nathan
08/07/2025, 7:16 PMprrao
08/07/2025, 7:16 PMgemma3:12b πprrao
08/07/2025, 7:17 PMprrao
08/07/2025, 7:17 PMprrao
08/07/2025, 7:17 PMprrao
08/07/2025, 7:18 PMPaco Nathan
08/07/2025, 7:18 PMprrao
08/07/2025, 7:18 PMprrao
08/07/2025, 7:18 PMPaco Nathan
08/07/2025, 7:19 PMTraceback (most recent call last):
File "/Users/paco/src/strwythura/rag.py", line 39, in <module>
response: baml_types.Response = rag.qa_cycle(
^^^^^^^^^^^^^
File "/Users/paco/src/strwythura/strwythura/strw.py", line 508, in qa_cycle
response: baml_types.Response = b.RAG(
^^^^^^
File "/Users/paco/src/strwythura/strwythura/baml_client/sync_client.py", line 94, in RAG
return typing.cast(types.Response, result.cast_to(types, types, stream_types, False, __runtime__))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
baml_py.internal_monkeypatch.BamlValidationError: BamlValidationError(message=Failed to parse LLM response: Failed to coerce value: <root>: Failed while parsing required fields: missing=0, unparsed=1
- <root>: Failed to parse field question: question: Expected string, got null
- question: Expected string, got null, raw_output={
"question": null,
"answer": "As of January 15, 2025, the results were published online in Neurology, the medical journal of the American Academy of Neurology."
}, prompt=system: Answer the question in full sentences using the provided context.
Do not make up an answer. If the information is not provided in the context, say so clearly.
QUESTION: dementia and tofu
RELEVANT CONTEXT: The findings also suggest that people can decrease their risk of dementia by replacing one serving of processed red meat with one serving of nuts and legumes, such as beans and peas, every day.
'Prevention of Alzheimer's disease and all other dementia is a major focus, and the Alzheimer's Association has long encouraged eating a healthier diet ' including foods that are less processed ' because they've been associated with lower the risk of cognitive decline,' said Heather M. Snyder, Ph.D., Alzheimer's Association vice president of medical and scientific relations.prrao
08/07/2025, 7:19 PMPaco Nathan
08/07/2025, 7:19 PMbaml_py.internal_monkeypatch.BamlValidationErroraaronv (BAML)
08/07/2025, 7:20 PMPaco Nathan
08/07/2025, 7:20 PMprrao
08/07/2025, 7:20 PMprrao
08/07/2025, 7:20 PMVaibhav (BAML π)
08/07/2025, 7:23 PM{
"question": null,
"answer": "As of January 15, 2025, the results were published online in Neurology, the medical journal of the American Academy of Neurology."
}
The llm is choosing to fill question with null but your type requires question to be a string.
a few ideas here:
1. make question options -- use string | null type
2. add a description for question: @description("rephrased more directly from what the user asked")
3. use a smarter modelprrao
08/07/2025, 7:24 PMgpt-4.1-miniprrao
08/07/2025, 7:25 PMprrao
08/07/2025, 7:25 PMVaibhav (BAML π)
08/07/2025, 7:25 PMprrao
08/07/2025, 7:25 PMVaibhav (BAML π)
08/07/2025, 7:25 PMVaibhav (BAML π)
08/07/2025, 7:25 PMprrao
08/07/2025, 7:25 PMprrao
08/07/2025, 7:25 PMPaco Nathan
08/07/2025, 7:26 PMprrao
08/07/2025, 7:26 PMprrao
08/07/2025, 7:27 PMVaibhav (BAML π)
08/07/2025, 7:27 PMVaibhav (BAML π)
08/07/2025, 7:27 PMprrao
08/07/2025, 7:28 PMstring | null helpsVaibhav (BAML π)
08/07/2025, 7:28 PM{
"question": null,
"answer": "As of January 15, 2025, the results were published online in Neurology, the medical journal of the American Academy of Neurology."
}
the llm produced teh null value for the field questionVaibhav (BAML π)
08/07/2025, 7:28 PMVaibhav (BAML π)
08/07/2025, 7:28 PMnull => string is not possibleprrao
08/07/2025, 7:28 PMPaco Nathan
08/07/2025, 7:28 PMquestion and context into the b.RAG() call.
But then somehow internally the value of question gets changed.prrao
08/07/2025, 7:29 PMprrao
08/07/2025, 7:29 PMPaco Nathan
08/07/2025, 7:29 PMprrao
08/07/2025, 7:29 PMVaibhav (BAML π)
08/07/2025, 7:29 PMsystem: Answer the question in full sentences using the provided context.
Do not make up an answer. If the information is not provided in the context, say so clearly.
QUESTION: dementia and tofu
RELEVANT CONTEXT: The findings also suggest that people can decrease their risk of dementia by replacing one serving of processed red meat with one serving of nuts and legumes, such as beans and peas, every day.
'Prevention of Alzheimer's disease and all other dementia is a major focus, and the Alzheimer's Association has long encouraged eating a healthier diet ' including foods that are less processed ' because they've been associated with lower the risk of cognitive decline,' said Heather M. Snyder, Ph.D., Alzheimer's Association vice president of medical and scientific relations.prrao
08/07/2025, 7:30 PMPaco Nathan
08/07/2025, 7:30 PMprrao
08/07/2025, 7:30 PMprrao
08/07/2025, 7:30 PMprrao
08/07/2025, 7:31 PMprrao
08/07/2025, 7:33 PMprrao
08/07/2025, 7:33 PMprrao
08/07/2025, 7:33 PMVaibhav (BAML π)
08/07/2025, 7:33 PMtype NonEmptyString = string @assert({{ this | length > 0}})
function Foo(question: NonEmptyString)Vaibhav (BAML π)
08/07/2025, 7:34 PMPaco Nathan
08/07/2025, 7:34 PMprrao
08/07/2025, 7:34 PMprrao
08/07/2025, 7:34 PMprrao
08/07/2025, 7:34 PMaaronv (BAML)
08/07/2025, 7:34 PMaaronv (BAML)
08/07/2025, 7:35 PMaaronv (BAML)
08/07/2025, 7:35 PMPaco Nathan
08/07/2025, 7:35 PMprrao
08/07/2025, 7:35 PMprrao
08/07/2025, 7:35 PMprrao
08/07/2025, 7:35 PMVaibhav (BAML π)
08/07/2025, 7:36 PMpython
question: str = "something user generated"
result = b.Raw(question)
# this sadly can't be guaranteed
assert result.question == questionVaibhav (BAML π)
08/07/2025, 7:36 PMprrao
08/07/2025, 7:37 PMprrao
08/07/2025, 7:37 PMprrao
08/07/2025, 7:37 PMVaibhav (BAML π)
08/07/2025, 7:38 PM{{ question or "<intentionally blank>" }}Vaibhav (BAML π)
08/07/2025, 7:38 PMPaco Nathan
08/07/2025, 7:39 PMVaibhav (BAML π)
08/07/2025, 7:39 PMpython
question: str = "something user generated"
result = b.Raw(question)
# this sadly can't be guaranteed
if result.question.lower() != question.lower():
if not b.IsSemanticallySame(question, result.question):
raise Exception("LLM is likely hallucinating!")
# otherwise we goodprrao
08/07/2025, 7:40 PMrs
test RAG1 {
functions [RAGTest]
args {
question "How many people were in the study?"
context "According to the study, eating a daily average of one-quarter or more of a serving of processed red meats (roughly two slices of bacon, one and half slices of bologna, or a hot dog), compared to those consuming a minimal amount (less than one-tenth a serving each day), had a 13 percent higher risk of developing dementia."
}
}
test RAG2 {
functions [RAGTest]
args {
question "What is the association between processed red meat and dementia?"
context "According to the study, eating a daily average of one-quarter or more of a serving of processed red meats (roughly two slices of bacon, one and half slices of bologna, or a hot dog), compared to those consuming a minimal amount (less than one-tenth a serving each day), had a 13 percent higher risk of developing dementia."
}
}
test RAG3 {
functions [RAGTest]
args {
question ""
context "According to the study, eating a daily average of one-quarter or more of a serving of processed red meats (roughly two slices of bacon, one and half slices of bologna, or a hot dog), compared to those consuming a minimal amount (less than one-tenth a serving each day), had a 13 percent higher risk of developing dementia."
}
}prrao
08/07/2025, 7:40 PMbaml_src/rag.baml (there's already a test there in that file on the GitHub repo, just update the function name accordingly)prrao
08/07/2025, 7:40 PMprrao
08/07/2025, 7:40 PMPaco Nathan
08/07/2025, 7:40 PMprrao
08/07/2025, 7:40 PMVaibhav (BAML π)
08/07/2025, 7:40 PMclass Answer {
qusetion_type "verbaitm" | "reworded" | "new"
question string
answer string
}Vaibhav (BAML π)
08/07/2025, 7:41 PMquestion_type == "new"prrao
08/07/2025, 7:42 PMVaibhav (BAML π)
08/07/2025, 7:42 PMprrao
08/07/2025, 8:02 PMprrao
08/07/2025, 8:03 PMValueError in the application code), the LLM will hallucinate and make up a question. The prompt doesn't protect you, and the LLM is primed to want to help, so it does something to get you a response. The fact that the prompt doesn't protect you from outright hallucination like this is a wakeup call to me, gonna be very wary about evals and empty strings any time LLMs are involved from this point π
Paco Nathan
08/07/2025, 8:49 PMassert on the question value to prevent any null questions
- added the question_type field which @Vaibhav (BAML π) suggested
- moved the Question below Context in the prompt
- now using gemma3:12b model instead of Llama3
Seems to work much better. Gemma3 still rewrites questions, just not as poorly as Llama3. Runs a bit slower.
If you want to run an illustration of enhanced GraphRAG -- using a fully annotated lexical graph based on spaCy + GLiNER, semantic expansions, semantic random walks, etc., to rerank the chunks -- which looks pretty cool when you see the traces of runs!
code:
https://github.com/DerwenAI/strwythura
call to BAML:
https://github.com/DerwenAI/strwythura/blob/c3f23ce5151b0b46ce566e6333ab635cad7a9553/strwythura/strw.py#L508
BAML source:
https://github.com/DerwenAI/strwythura/blob/main/strwythura/baml_src/rag.baml
I cannot see anywhere that the Python code would be introducing an error -- unless somehow I'm not calling BAML correctly?
response: baml_types.Response = b.RAG(
question,
context,
)prrao
08/08/2025, 2:18 AMprrao
08/08/2025, 2:18 AMVaibhav (BAML π)
08/08/2025, 5:14 AMVaibhav (BAML π)
08/08/2025, 5:14 AMprrao
08/08/2025, 5:51 PMprrao
08/08/2025, 5:52 PM{{ question }} parameter in the part after the user message begins. Specifically, in this order:
.., (rest of the system messsage up here)
{{ ctx.output_format }}
{{ _.role("user") }}
QUESTION: {{ question }}prrao
08/08/2025, 5:54 PM{{ ctx.output_format }}Paco Nathan
08/08/2025, 11:40 PM