refactor: improve artifact type handling and result processing (#6002)

🐛 (component.py): fix the logic to determine the artifact type based on raw data and status
🐛 (artifact.py): fix the default message assignment in post_process_raw function to ensure consistent behavior
This commit is contained in:
Cristhian Zanforlin Lousa 2025-01-29 16:43:20 -03:00 committed by GitHub
commit 7e756b9db5
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 8 additions and 6 deletions

View file

@ -957,19 +957,19 @@ class Component(CustomComponent):
custom_repr = str(custom_repr)
raw = self._process_raw_result(result)
artifact_type = get_artifact_type(self.status or raw, result)
artifact_type = get_artifact_type(raw or self.status, result)
raw, artifact_type = post_process_raw(raw, artifact_type)
return {"repr": custom_repr, "raw": raw, "type": artifact_type}
def _process_raw_result(self, result):
if self.status:
raw = self.status
elif hasattr(result, "data"):
if hasattr(result, "data"):
raw = result.data
elif hasattr(result, "model_dump"):
raw = result.model_dump()
elif isinstance(result, dict | Data | str):
raw = result.data if isinstance(result, Data) else result
elif self.status:
raw = self.status
else:
raw = result
return raw

View file

@ -63,6 +63,8 @@ def _to_list_of_dicts(raw):
def post_process_raw(raw, artifact_type: str):
default_message = "Built Successfully ✨"
if artifact_type == ArtifactType.STREAM.value:
raw = ""
elif artifact_type == ArtifactType.ARRAY.value:
@ -74,7 +76,7 @@ def post_process_raw(raw, artifact_type: str):
artifact_type = ArtifactType.OBJECT.value
except Exception: # noqa: BLE001
logger.opt(exception=True).debug(f"Error converting to json: {raw} ({type(raw)})")
raw = "Built Successfully ✨"
raw = default_message
else:
raw = "Built Successfully ✨"
raw = default_message
return raw, artifact_type