feat: offload jinja to threadpool LET-3615 (#3787)

This commit is contained in:
cthomas
2025-08-06 16:34:28 -07:00
committed by GitHub
parent fd9dc0c1f8
commit 9a2caeb0bc
7 changed files with 25 additions and 11 deletions

View File

@@ -1,3 +1,4 @@
import asyncio
import logging
from typing import TYPE_CHECKING, List, Optional
@@ -142,11 +143,11 @@ class Memory(BaseModel, validate_assignment=True):
"""
try:
# Validate Jinja2 syntax with async enabled
Template(prompt_template, enable_async=True)
Template(prompt_template)
# Validate compatibility with current memory structure - use async rendering
template = Template(prompt_template, enable_async=True)
await template.render_async(blocks=self.blocks, file_blocks=self.file_blocks, sources=[], max_files_open=None)
template = Template(prompt_template)
await asyncio.to_thread(template.render, blocks=self.blocks, file_blocks=self.file_blocks, sources=[], max_files_open=None)
# If we get here, the template is valid and compatible
self.prompt_template = prompt_template
@@ -189,6 +190,11 @@ class Memory(BaseModel, validate_assignment=True):
except Exception as e:
raise ValueError(f"Prompt template is not compatible with current memory structure: {str(e)}")
@trace_method
async def compile_in_thread_async(self, tool_usage_rules=None, sources=None, max_files_open=None) -> str:
"""Compile the memory in a thread"""
return await asyncio.to_thread(self.compile, tool_usage_rules=tool_usage_rules, sources=sources, max_files_open=max_files_open)
def list_block_labels(self) -> List[str]:
"""Return a list of the block names held inside the memory object"""
# return list(self.memory.keys())