wendaolee
07/29/2025, 8:02 AMruntime of BAML ?
I'm wrting a function like this in my project :
typescript
export const chatWithLLMByBAMLSAPUnderOpenAIFormat = <T>(parser:(t:string) => T,req:any) => Effect.tryPromise({
try:async () => {
const openai = new OpenAI({
baseURL:BASEURL,
apiKey:APIKEY
})
/**
* [example](https://docs.boundaryml.com/guide/baml-advanced/modular-api#openai-chat-completions-api)
*/
const res = await openai.chat.completions.create(req)
const parsedResult = parser(res.choices[0].message.content!)
return {
result:parsedResult,
model:res.model,
usage:res.usage
}
},
catch:(e) => {
if(e instanceof BamlValidationError){
return e
}
return openaiErrorMatcher('chatWithLLMByBAMLSAPUnderOpenAIFormat')(e as any)
}
})wendaolee
07/29/2025, 8:04 AMtypescript
const eff = chatWithLLMByBAMLSAPUnderOpenAIFormat(b.parse.Memene_Top3RelatedExtract,Sample)
When running code,I met this error:
TypeError: Cannot read properties of undefined (reading \'runtime\')\n' +
' at Memene_Top3RelatedExtract (D:\Backup\Documents\GitHub\Content-crawling-generation-system\src\baml\baml_client\parser.ts:134:19)\n
And the code here are:
typescript
Memene_Top3RelatedExtract(
llmResponse: string,
__baml_options__?: { tb?: TypeBuilder, clientRegistry?: ClientRegistry, env?: Record<string, string | undefined> }
): types.Memene_Top3Related {
try {
const rawEnv = __baml_options__?.env ? { ...process.env, ...__baml_options__.env } : { ...process.env };
const env: Record<string, string> = Object.fromEntries(
Object.entries(rawEnv).filter(([_, value]) => value !== undefined) as [string, string][]
);
return this.runtime.parseLlmResponse(
"Memene_Top3RelatedExtract",
llmResponse,
false,
this.ctxManager.cloneContext(),
__baml_options__?.tb?.__tb(),
__baml_options__?.clientRegistry,
env,
) as types.Memene_Top3Related
} catch (error) {
throw toBamlError(error);
}
}
What things make this error?wendaolee
07/29/2025, 8:09 AMconst parsedResult = parser(res.choices[0].message.content!).wendaolee
07/29/2025, 8:20 AMinlinebaml.ts .Fine design.
But why I met runtime no defined error?Seems my code all okay.
https://cdn.discordapp.com/attachments/1399663347871518791/1399667909831299082/image.png?ex=6889d5d2&is=68888452&hm=950e1596a7adc238ed43054566810820007d4a09e5c281dd79ca499352248319&wendaolee
07/29/2025, 8:46 AMthis binding.See below code:
const t = b.parse.Memene_Top3RelatedExtract("{isRelated:false}")
const parser = b.parse.Memene_Top3RelatedExtract.bind(b.parse) // this will success because it use bind
const result = parser("{isRelated:false}")
If you just use parse.func ,because the function rely on this to get the exact runtime message (I think the major message is the dsl definition):
typescript
Memene_Top3RelatedExtract(
llmResponse: string,
__baml_options__?: { tb?: TypeBuilder, clientRegistry?: ClientRegistry, env?: Record<string, string | undefined> }
): types.Memene_Top3Related {
try {
const rawEnv = __baml_options__?.env ? { ...process.env, ...__baml_options__.env } : { ...process.env };
const env: Record<string, string> = Object.fromEntries(
Object.entries(rawEnv).filter(([_, value]) => value !== undefined) as [string, string][]
);
//here
return this.runtime.parseLlmResponse(
"Memene_Top3RelatedExtract",
llmResponse,
false,
this.ctxManager.cloneContext(),
__baml_options__?.tb?.__tb(),
__baml_options__?.clientRegistry,
env,
) as types.Memene_Top3Related
} catch (error) {
throw toBamlError(error);
}
}
So the this.runtime will point to undefined because it not derived from a exact instance.wendaolee
07/29/2025, 8:47 AM.bind.Ah,thanks to JavaScript:The good parts ,the book I read 5 years ago. I finaly know the reason.๐SO funny.Vaibhav (BAML ๐)
07/29/2025, 1:49 PMVaibhav (BAML ๐)
07/29/2025, 1:49 PMVaibhav (BAML ๐)
07/29/2025, 1:50 PM