Hi everyone, I am struggling to safe an agent response from a tool in the following PydanticBase Model:
class LehrplanCompetencyMap(BaseModel):
""" Strukturierte Darstellung der Kompetenzen eines Lehrplans """
school_state: str = Field("", description="Bundesland des Lehrplans")
school_level: str = Field("", description="Schulstufe des Lehrplans")
class_level: int = Field(0, description="Angegebene Klassenstufe")
subject_competencies: Dict[str, Dict[str, List[str]]] = Field(
default_factory=Dict, description="Strukturierte Darstellung der Kompetenzen eines Lehrplans")
Running agentic workflow with the tool:
async def query_lehrplan_competencies(ctx: Context, query: str) -> LehrplanCompetencyMap:
"Extrahiert Kompetenzen aus dem Lehrplan und speichert sie Schema im Context State."
response = await lehrplaene_engine.aquery(query)
try:
lehrplan_data = LehrplanCompetencyMap.model_validate(response)
except Exception as e:
print(f"Fehler beim Parsen der Lehrplan-Daten: {e}")
return LehrplanCompetencyMap()
current_state = await ctx.get("state")
if "lehrplan_data" not in current_state:
current_state["lehrplan_data"] = {}
if "subject_competencies" not in current_state["lehrplan_data"]:
current_state["lehrplan_data"]["subject_competencies"] = {}
current_state["lehrplan_data"] = lehrplan_data.model_dump()
await ctx.set("state", current_state)
return lehrplan_data # Rückgabe des Pydantic-Modells
The subject comptency is not stored properly in the PydanticModel & context with this error:
🔧 Tool Result (query_lehrplan_competencies):
Arguments: {'query': 'Mathematik 2. Klasse Bayern'}
Output: 1 validation error for LehrplanCompetencyMap
Invalid JSON: expected value at line 1 column 1 [type=json_invalid, input_value='Type Dict cannot be instted; use dict() instead', input_type=str] For further information visit
https://errors.pydantic.dev/2.8/v/json_invalid