Skip to content
32 changes: 32 additions & 0 deletions src/backend/base/langflow/api/v1/endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,38 @@
SimplifiedAPIRequest: Parsed request or default instance if parsing fails
"""
try:
content_type = (http_request.headers.get("content-type") or "").lower()

# If multipart/form-data, use request.form() so uploaded files are not consumed
if "multipart/form-data" in content_type:
form = await http_request.form()
data: dict = {}

# Map expected form fields to the SimplifiedAPIRequest schema
if "input_value" in form:
data["input_value"] = form.get("input_value")
if "input_type" in form:
data["input_type"] = form.get("input_type")
if "output_type" in form:
data["output_type"] = form.get("output_type")
if "output_component" in form:
data["output_component"] = form.get("output_component")
if "session_id" in form:
data["session_id"] = form.get("session_id")

# Tweaks may be JSON encoded in a form field
if "tweaks" in form:
raw_tweaks = form.get("tweaks")
if raw_tweaks:
try:
data["tweaks"] = orjson.loads(raw_tweaks) if isinstance(raw_tweaks, (str, bytes)) else raw_tweaks

Check failure on line 100 in src/backend/base/langflow/api/v1/endpoints.py

View workflow job for this annotation

GitHub Actions / Ruff Style Check (3.13)

Ruff (E501)

src/backend/base/langflow/api/v1/endpoints.py:100:121: E501 Line too long (121 > 120)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🔴 Critical

Fix line length to pass pipeline check.

Line 100 exceeds the 120-character limit, causing the Ruff style check to fail.

Apply this diff to split the line:

-                        data["tweaks"] = orjson.loads(raw_tweaks) if isinstance(raw_tweaks, (str, bytes)) else raw_tweaks
+                        data["tweaks"] = (
+                            orjson.loads(raw_tweaks) if isinstance(raw_tweaks, (str, bytes)) else raw_tweaks
+                        )
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
data["tweaks"] = orjson.loads(raw_tweaks) if isinstance(raw_tweaks, (str, bytes)) else raw_tweaks
data["tweaks"] = (
orjson.loads(raw_tweaks) if isinstance(raw_tweaks, (str, bytes)) else raw_tweaks
)
🧰 Tools
🪛 GitHub Actions: Ruff Style Check

[error] 100-100: ruff check failed: E501 Line too long (121 > 120) in src/backend/base/langflow/api/v1/endpoints.py. Command: uv run --only-dev ruff check --output-format=github .

🪛 GitHub Check: Ruff Style Check (3.13)

[failure] 100-100: Ruff (E501)
src/backend/base/langflow/api/v1/endpoints.py:100:121: E501 Line too long (121 > 120)

🤖 Prompt for AI Agents
In src/backend/base/langflow/api/v1/endpoints.py around line 100, the single
line assigning data["tweaks"] exceeds the 120-character limit; break it into
multiple shorter lines so it fits the style guide. Specifically, compute
orjson.loads(raw_tweaks) only when raw_tweaks is a str or bytes and assign to
data["tweaks"] using a conditional expression split across lines (or use an
intermediate variable) so no line is longer than 120 characters and behavior
remains identical.

except Exception:

Check failure on line 101 in src/backend/base/langflow/api/v1/endpoints.py

View workflow job for this annotation

GitHub Actions / Ruff Style Check (3.13)

Ruff (BLE001)

src/backend/base/langflow/api/v1/endpoints.py:101:28: BLE001 Do not catch blind exception: `Exception`
# Leave as raw value if parsing fails
data["tweaks"] = raw_tweaks
Comment on lines 99 to 108
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

Improve exception handling and add type validation.

Two issues:

  1. The blind Exception catch on line 101 is too broad.
  2. Form fields might be UploadFile objects rather than strings if a client uploads a file under a field name like tweaks. This could cause downstream validation errors.

Apply this diff:

             # Tweaks may be JSON encoded in a form field
             if "tweaks" in form:
                 raw_tweaks = form.get("tweaks")
-                if raw_tweaks:
+                if raw_tweaks and isinstance(raw_tweaks, (str, bytes)):
                     try:
-                        data["tweaks"] = (
-                            orjson.loads(raw_tweaks) if isinstance(raw_tweaks, (str, bytes)) else raw_tweaks
-                        )
-                    except Exception:
+                        data["tweaks"] = orjson.loads(raw_tweaks)
+                    except (ValueError, orjson.JSONDecodeError):
                         # Leave as raw value if parsing fails
                         data["tweaks"] = raw_tweaks
🧰 Tools
🪛 GitHub Actions: Ruff Style Check

[error] 100-100: ruff check failed: E501 Line too long (121 > 120) in src/backend/base/langflow/api/v1/endpoints.py. Command: uv run --only-dev ruff check --output-format=github .

🪛 GitHub Check: Ruff Style Check (3.13)

[failure] 101-101: Ruff (BLE001)
src/backend/base/langflow/api/v1/endpoints.py:101:28: BLE001 Do not catch blind exception: Exception


[failure] 100-100: Ruff (E501)
src/backend/base/langflow/api/v1/endpoints.py:100:121: E501 Line too long (121 > 120)


return SimplifiedAPIRequest(**data)

# Fallback: parse as JSON (application/json or others)
body = await http_request.body()
if body:
body_data = orjson.loads(body)
Expand Down
Loading