Custom serialization handling circular references and filtering sensitive data
(self, sensitive_data: dict[str, str | dict[str, str]] | None = None, **kwargs)
| 548 | return filtered_data |
| 549 | |
| 550 | def model_dump(self, sensitive_data: dict[str, str | dict[str, str]] | None = None, **kwargs) -> dict[str, Any]: |
| 551 | """Custom serialization handling circular references and filtering sensitive data""" |
| 552 | |
| 553 | # Handle action serialization |
| 554 | model_output_dump = None |
| 555 | if self.model_output: |
| 556 | action_dump = [action.model_dump(exclude_none=True, mode='json') for action in self.model_output.action] |
| 557 | |
| 558 | # Filter sensitive data only from input action parameters if sensitive_data is provided |
| 559 | if sensitive_data: |
| 560 | action_dump = [ |
| 561 | self._filter_sensitive_data_from_dict(action, sensitive_data) if 'input' in action else action |
| 562 | for action in action_dump |
| 563 | ] |
| 564 | |
| 565 | model_output_dump = { |
| 566 | 'evaluation_previous_goal': self.model_output.evaluation_previous_goal, |
| 567 | 'memory': self.model_output.memory, |
| 568 | 'next_goal': self.model_output.next_goal, |
| 569 | 'action': action_dump, # This preserves the actual action data |
| 570 | } |
| 571 | # Only include thinking if it's present |
| 572 | if self.model_output.thinking is not None: |
| 573 | model_output_dump['thinking'] = self.model_output.thinking |
| 574 | if self.model_output.current_plan_item is not None: |
| 575 | model_output_dump['current_plan_item'] = self.model_output.current_plan_item |
| 576 | if self.model_output.plan_update is not None: |
| 577 | model_output_dump['plan_update'] = self.model_output.plan_update |
| 578 | |
| 579 | # Handle result serialization - don't filter ActionResult data |
| 580 | # as it should contain meaningful information for the agent |
| 581 | result_dump = [r.model_dump(exclude_none=True, mode='json') for r in self.result] |
| 582 | |
| 583 | return { |
| 584 | 'model_output': model_output_dump, |
| 585 | 'result': result_dump, |
| 586 | 'state': self.state.to_dict(), |
| 587 | 'metadata': self.metadata.model_dump() if self.metadata else None, |
| 588 | 'state_message': self.state_message, |
| 589 | } |
| 590 | |
| 591 | |
| 592 | AgentStructuredOutput = TypeVar('AgentStructuredOutput', bound=BaseModel) |
no test coverage detected