Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

⚡️ Speed up function build_output_logs by 11% in PR #6028 (PlaygroundPage) #6161

Open
wants to merge 2 commits into
base: PlaygroundPage
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 35 additions & 55 deletions src/backend/base/langflow/schema/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from langflow.schema.data import Data
from langflow.schema.dataframe import DataFrame
from langflow.schema.message import Message
from langflow.serialization.serialization import serialize
from langflow.serialization.serialization import serialize as original_serialize

INPUT_FIELD_NAME = "input_value"

Expand Down Expand Up @@ -41,77 +41,57 @@


def get_type(payload):
result = LogType.UNKNOWN
match payload:
case Message():
result = LogType.MESSAGE

case Data():
result = LogType.DATA

case dict():
result = LogType.OBJECT

case list() | DataFrame():
result = LogType.ARRAY

case str():
result = LogType.TEXT

if result == LogType.UNKNOWN and (
(payload and isinstance(payload, Generator))
or (isinstance(payload, Message) and isinstance(payload.text, Generator))
):
result = LogType.STREAM

return result
if isinstance(payload, Message):
return LogType.MESSAGE
if isinstance(payload, Data):
return LogType.DATA
if isinstance(payload, dict):
return LogType.OBJECT
if isinstance(payload, (list, DataFrame)):

Check failure on line 50 in src/backend/base/langflow/schema/schema.py

View workflow job for this annotation

GitHub Actions / Ruff Style Check (3.12)

Ruff (UP038)

src/backend/base/langflow/schema/schema.py:50:8: UP038 Use `X | Y` in `isinstance` call instead of `(X, Y)`
return LogType.ARRAY
if isinstance(payload, str):
return LogType.TEXT
if isinstance(payload, Generator) or (isinstance(payload, Message) and isinstance(payload.text, Generator)):
return LogType.STREAM
return LogType.UNKNOWN


def get_message(payload):
message = None
if hasattr(payload, "data"):
message = payload.data

elif hasattr(payload, "model_dump"):
message = payload.model_dump()

if message is None and isinstance(payload, dict | str | Data):
message = payload.data if isinstance(payload, Data) else payload

return message or payload
return payload.data
if hasattr(payload, "model_dump"):
return payload.model_dump()
if isinstance(payload, (dict, str, Data)):

Check failure on line 64 in src/backend/base/langflow/schema/schema.py

View workflow job for this annotation

GitHub Actions / Ruff Style Check (3.12)

Ruff (UP038)

src/backend/base/langflow/schema/schema.py:64:8: UP038 Use `X | Y` in `isinstance` call instead of `(X, Y)`
return payload.data if isinstance(payload, Data) else payload
return payload


def build_output_logs(vertex, result) -> dict:
outputs: dict[str, OutputValue] = {}
outputs = {}
component_instance = result[0]
for index, output in enumerate(vertex.outputs):
if component_instance.status is None:
payload = component_instance._results
output_result = payload.get(output["name"])
else:
payload = component_instance._artifacts
output_result = payload.get(output["name"], {}).get("raw")
payload = component_instance._results if component_instance.status is None else component_instance._artifacts
output_result = (
payload.get(output["name"], {}).get("raw") if component_instance.status else payload.get(output["name"])
)
message = get_message(output_result)
type_ = get_type(output_result)

match type_:
case LogType.STREAM if "stream_url" in message:
if type_ == LogType.STREAM:
if isinstance(message, dict) and "stream_url" in message:
message = StreamURL(location=message["stream_url"])

case LogType.STREAM:
else:
message = ""

case LogType.MESSAGE if hasattr(message, "message"):
message = message.message
elif type_ == LogType.MESSAGE and hasattr(message, "message"):
message = message.message

case LogType.UNKNOWN:
message = ""
elif type_ in {LogType.UNKNOWN, LogType.ARRAY}:
if isinstance(message, DataFrame):
message = message.to_dict(orient="records")
message = [original_serialize(item) for item in message]

case LogType.ARRAY:
if isinstance(message, DataFrame):
message = message.to_dict(orient="records")
message = [serialize(item) for item in message]
name = output.get("name", f"output_{index}")
outputs |= {name: OutputValue(message=message, type=type_).model_dump()}
outputs[name] = OutputValue(message=message, type=type_).model_dump()

return outputs
37 changes: 8 additions & 29 deletions src/backend/base/langflow/serialization/serialization.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,49 +226,28 @@
*,
to_str: bool = False,
) -> Any:
"""Unified serialization with optional truncation support.

Coordinates specialized serializers through a dispatcher pattern.
Maintains recursive processing for nested structures.

Args:
obj: Object to serialize
max_length: Maximum length for string values, None for no truncation
max_items: Maximum items in list-like structures, None for no truncation
to_str: If True, return a string representation of the object if serialization fails
"""
"""Unified serialization with optional truncation support."""
if obj is None:
return None
try:
# First try type-specific serialization
result = _serialize_dispatcher(obj, max_length, max_items)
if result is not UNSERIALIZABLE_SENTINEL: # Special check for None since it's a valid result
if result is not UNSERIALIZABLE_SENTINEL:
return result

# Handle class-based Pydantic types and other types
if isinstance(obj, type):
if issubclass(obj, BaseModel | BaseModelV1):
return repr(obj)
return str(obj) # Handle other class types

# Handle type aliases and generic types
if hasattr(obj, "__origin__") or hasattr(obj, "__parameters__"): # Type alias or generic type check
try:
return repr(obj)
except Exception as e: # noqa: BLE001
logger.debug(f"Cannot serialize object {obj}: {e!s}")

# Fallback to common serialization patterns
return repr(obj) if issubclass(obj, (BaseModel, BaseModelV1)) else str(obj)

Check failure on line 238 in src/backend/base/langflow/serialization/serialization.py

View workflow job for this annotation

GitHub Actions / Ruff Style Check (3.12)

Ruff (UP038)

src/backend/base/langflow/serialization/serialization.py:238:33: UP038 Use `X | Y` in `issubclass` call instead of `(X, Y)`

if hasattr(obj, "__origin__") or hasattr(obj, "__parameters__"):
return repr(obj)

if hasattr(obj, "model_dump"):
return serialize(obj.model_dump(), max_length, max_items)
if hasattr(obj, "dict") and not isinstance(obj, type):
return serialize(obj.dict(), max_length, max_items)

# Final fallback to string conversion only if explicitly requested
if to_str:
return str(obj)

except Exception as e: # noqa: BLE001
except Exception as e:

Check failure on line 250 in src/backend/base/langflow/serialization/serialization.py

View workflow job for this annotation

GitHub Actions / Ruff Style Check (3.12)

Ruff (BLE001)

src/backend/base/langflow/serialization/serialization.py:250:12: BLE001 Do not catch blind exception: `Exception`
logger.debug(f"Cannot serialize object {obj}: {e!s}")
return "[Unserializable Object]"
return obj
Expand Down
Loading