Skip to content

fix: strip reserved 'code' param before build() in component reuse path#12712

Merged
erichare merged 4 commits into
langflow-ai:release-1.10.0from
aayushbaluni:fix/8610-strip-code-from-build-params
May 11, 2026
Merged

fix: strip reserved 'code' param before build() in component reuse path#12712
erichare merged 4 commits into
langflow-ai:release-1.10.0from
aayushbaluni:fix/8610-strip-code-from-build-params

Conversation

@aayushbaluni

@aayushbaluni aayushbaluni commented Apr 15, 2026

Copy link
Copy Markdown
Contributor

Summary

Fixes #8610 and the duplicate report at #13067.

Root cause: When a custom component is reused (self.custom_component already exists on the vertex), the else branch of Vertex._build calls get_params(self.params) and passes the result straight into build_custom_component, which calls build(**params). Unlike the instantiate_class path — which pops code before use — the reuse path leaks the synthetic code template field through to build(), raising:

TypeError: <Component>.build() got an unexpected keyword argument 'code'

for any legacy CustomComponent whose build() signature doesn't accept **kwargs (e.g. a custom MiddlewareComponent / PolicyCheck with def build(self, api_key, middleware_url, user_request)).

Modern Component subclasses use output methods and aren't affected, which is why this slipped through.

Fix

Three defensive params.pop("code", None) insertions so both code paths consistently strip the reserved code field before calling build():

  1. src/lfx/src/lfx/graph/vertex/base.py — reuse branch of _build, right after get_params().
  2. src/lfx/src/lfx/interface/initialize/loading.py — top of build_custom_component() (catches any other caller that forgets to strip).
  3. src/backend/base/langflow/interface/initialize/loading.py — same defensive strip in the parallel build_custom_component() copy. In production this duplicate is dead code (only update_params_with_load_from_db_fields is imported from it via endpoints.py), but keeping it in sync avoids future drift.

Testing

  • New regression tests in src/lfx/tests/unit/interface/test_loading_custom_component_code_param.py covering:
    • Strict sync build() signature with code in params.
    • Strict async build() signature with code in params.
    • code: None and missing-code variants (pop must be safe).
    • get_params reuse-path semantics.
  • All existing test_loading.py tests still pass.

Branch

Rebased onto release-1.10.0 (was previously on main) to target the active release branch.

Notes

This PR does not address the separate "Message → str" connection-compatibility complaint from #13067's bug report; that's a frontend type-system issue with a different root cause and should be triaged separately.

Made with Cursor

Summary by CodeRabbit

  • Bug Fixes
    • Resolved a parameter handling issue in custom component reuse that could cause incorrect values to persist across component initialization cycles.

@coderabbitai

coderabbitai Bot commented Apr 15, 2026

Copy link
Copy Markdown
Contributor

Walkthrough

This PR fixes a bug where the "code" parameter was being passed to custom component build() methods, causing TypeError. The fix removes the "code" entry at two key points in the component building pipeline before it reaches downstream build() invocations.

Changes

Cohort / File(s) Summary
Parameter Cleanup
src/lfx/src/lfx/graph/vertex/base.py, src/lfx/src/lfx/interface/initialize/loading.py
Removes the "code" key from params dictionaries using pop("code", None) before custom component building and result construction to prevent unexpected keyword argument errors.

Estimated code review effort

🎯 1 (Trivial) | ⏱️ ~3 minutes


Important

Pre-merge checks failed

Please resolve all errors before merging. Addressing warnings is optional.

❌ Failed checks (1 error, 4 warnings)

Check name Status Explanation Resolution
Test Coverage For New Implementations ❌ Error PR adds two defensive lines to fix TypeError when reusing custom components with code parameter, but includes no regression tests to verify the fix. Add regression tests verifying the TypeError no longer occurs when reusing custom components with the code parameter in both instantiate and reuse paths.
Out of Scope Changes check ⚠️ Warning The PR contains only the two minimal changes needed to strip 'code' from params. However, issue #8610 mentions a secondary problem about input/output type compatibility (Text Input to Text Field) that is not addressed here. The PR title and objectives mention addressing issue #8610, which includes a secondary type compatibility problem between Text Input and Text Field that remains unresolved in these changes.
Docstring Coverage ⚠️ Warning Docstring coverage is 50.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
Test Quality And Coverage ⚠️ Warning PR modifies critical async functions but includes zero new test coverage for the modified code paths. Add async pytest test cases in test_vertex_base.py and test_loading.py to verify the code parameter removal fix and prevent regression of issue #8610.
Test File Naming And Structure ⚠️ Warning PR adds 2 lines of production code to fix TypeError in MiddlewareComponent.build() but includes no new or updated test files to cover the changes. Add test files with descriptive test cases verifying code parameter removal in both instantiate and reuse paths, ensuring components no longer receive unexpected code kwargs.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and concisely describes the main fix: removing the 'code' parameter before build() in the component reuse path, which directly addresses the core issue.
Linked Issues check ✅ Passed The PR successfully addresses the primary requirement from issue #8610: preventing TypeError by removing the reserved 'code' parameter before calling build() in both component paths.
Excessive Mock Usage Warning ✅ Passed PR includes only two source files with minimal changes (+1/-0 each) and no test files are modified or added.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@github-actions github-actions Bot added bug Something isn't working and removed bug Something isn't working labels Apr 15, 2026
aayushbaluni added a commit to aayushbaluni/langflow that referenced this pull request Apr 15, 2026
Verifies that TypeError no longer occurs when reusing custom components
with the code parameter in both instantiate and reuse paths.

Per CodeRabbit review on PR langflow-ai#12712.

Made-with: Cursor
@github-actions github-actions Bot added bug Something isn't working and removed bug Something isn't working labels Apr 15, 2026
@aayushbaluni aayushbaluni force-pushed the fix/8610-strip-code-from-build-params branch from c8d48e4 to 447f375 Compare April 26, 2026 06:15
aayushbaluni added a commit to aayushbaluni/langflow that referenced this pull request Apr 26, 2026
Verifies that TypeError no longer occurs when reusing custom components
with the code parameter in both instantiate and reuse paths.

Per CodeRabbit review on PR langflow-ai#12712.

Made-with: Cursor
@github-actions github-actions Bot added bug Something isn't working and removed bug Something isn't working labels Apr 26, 2026
aayushbaluni and others added 4 commits May 11, 2026 08:46
…th (langflow-ai#8610)

Root cause: When a custom component is reused (self.custom_component already exists),
get_params() returns params that still include the reserved 'code' field. Unlike the
instantiate_class path which pops 'code' before use, the reuse path passes it through
to build_custom_component, which calls build(**params) — causing TypeError for
components whose build() method does not accept **kwargs.

Made-with: Cursor
Verifies that TypeError no longer occurs when reusing custom components
with the code parameter in both instantiate and reuse paths.

Per CodeRabbit review on PR langflow-ai#12712.

Made-with: Cursor
Mirror the lfx fix in the parallel src/backend/base/langflow/interface/
initialize/loading.py copy. In production this build_custom_component is
dead code (only update_params_with_load_from_db_fields is imported from
this module), but keeping the duplicate in sync avoids future drift and
the same TypeError if any caller starts using it.
@erichare erichare force-pushed the fix/8610-strip-code-from-build-params branch from 447f375 to e7f2597 Compare May 11, 2026 15:46
@erichare erichare changed the base branch from main to release-1.10.0 May 11, 2026 15:47
@github-actions github-actions Bot added bug Something isn't working and removed bug Something isn't working labels May 11, 2026
@erichare erichare self-requested a review May 11, 2026 15:48

@erichare erichare left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

LGTM, thank you @aayushbaluni

@erichare erichare enabled auto-merge May 11, 2026 16:01
@erichare erichare added this pull request to the merge queue May 11, 2026
Merged via the queue into langflow-ai:release-1.10.0 with commit 5c111a1 May 11, 2026
295 of 301 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

TypeError: MiddlewareComponent.build() got an unexpected keyword argument 'code'

2 participants