def apply_patch(self, patch: JsonPatch) -> dict:
        """
        Apply a JSON patch to the current Pydantic model instance.

        Args:
            patch: JsonPatch containing operations to apply

        Returns:
            New instance of the same model type with patches applied.
            Also overwrites and saves the new instance as the current artifact.

        Raises:
            ValueError: If patch operation is invalid
            IndexError: If array index is out of range
            ValidationError: If patch results in invalid model

        """
        # Validate patch object
        if isinstance(patch, dict):
            patch = JsonPatch.model_validate(patch)
        elif isinstance(patch, str):
            patch = JsonPatch.model_validate_json(patch)

        # Convert to dict for easier manipulation
        model_dict = self.current_artifact.model_dump()
        model_class = self.pydantic_cls

        for operation in patch.operations:
            try:
                self._apply_single_operation(model_dict, operation)
            except Exception as e:
                raise ValueError(
                    f"Failed to apply operation {operation.op} at {operation.path}: {e!s}"
                )

        # Convert back to original model type and validate
        try:
            self.current_artifact = model_class.model_validate(model_dict)
            return self.current_artifact.model_dump()
        except ValidationError as e:
            raise ValueError(
                f"Patch resulted in invalid {model_class.__name__} structure: {e!s}"
            )

        Apply a JSON patch to the current Pydantic model instance.

        Args:
            patch: JsonPatch containing operations to apply

        Returns:
            New instance of the same model type with patches applied.
            Also overwrites and saves the new instance as the current artifact.

        Raises:
            ValueError: If patch operation is invalid
            IndexError: If array index is out of range
            ValidationError: If patch results in invalid model

        
llama-index-integrations/tools/llama-index-tools-artifact-editor/llama_index/tools/artifact_editor/base.py