Skip to content

fix: coerce empty port string to None in create_graph_engine#2486

Open
soichisumi wants to merge 2 commits intotopoteretes:mainfrom
soichisumi:fix/port-coercion-empty-string
Open

fix: coerce empty port string to None in create_graph_engine#2486
soichisumi wants to merge 2 commits intotopoteretes:mainfrom
soichisumi:fix/port-coercion-empty-string

Conversation

@soichisumi
Copy link
Copy Markdown
Contributor

@soichisumi soichisumi commented Mar 26, 2026

Problem

When GRAPH_DATABASE_PORT is unset or empty, get_graph_context_config() returns graph_database_port="" (empty string). This value is passed directly to community adapters (via supported_databases dict), which may call int(port), causing:

ValueError: invalid literal for int() with base 10: ''

This affects FalkorDB and potentially other community adapters that expect a numeric port.

Fix

Coerce empty string and None to None before passing to adapters in _create_graph_engine(). This lets each adapter apply its own default port value (e.g. FalkorDB defaults to 6379).

Impact

  • Only affects the supported_databases code path (community adapters)
  • Built-in adapters (Neo4j, Kuzu, Neptune) are unaffected as they don't use the port parameter directly
  • No breaking changes — adapters already need to handle None port gracefully

Summary by CodeRabbit

  • Bug Fixes
    • Normalized empty or missing graph database port values so they’re treated consistently, preventing redundant cache entries and improving connection reliability.

When GRAPH_DATABASE_PORT is unset or empty, get_graph_context_config()
returns graph_database_port="" (empty string). This is passed directly
to community adapters which may call int(port), causing:

  ValueError: invalid literal for int() with base 10: ''

Coerce empty string and None to None before passing to adapters, so
they can apply their own default port values.
@pull-checklist
Copy link
Copy Markdown

Please make sure all the checkboxes are checked:

  • I have tested these changes locally.
  • I have reviewed the code changes.
  • I have added end-to-end and unit tests (if applicable).
  • I have updated the documentation and README.md file (if necessary).
  • I have removed unnecessary code and debug statements.
  • PR title is clear and follows the convention.
  • I have tagged reviewers or team members for feedback.

@github-actions
Copy link
Copy Markdown

Hello @soichisumi, thank you for submitting a PR! We will respond as soon as possible.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai bot commented Mar 26, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 210a62b1-b6f5-45bd-aa26-d6efceec3da1

📥 Commits

Reviewing files that changed from the base of the PR and between 650131b and 53076d8.

📒 Files selected for processing (1)
  • cognee/infrastructure/databases/graph/get_graph_engine.py
🚧 Files skipped from review as they are similar to previous changes (1)
  • cognee/infrastructure/databases/graph/get_graph_engine.py

Walkthrough

create_graph_engine now normalizes graph_database_port by converting empty string ("") or None to None before calling _create_graph_engine, preventing different cache keys for "" vs None at the @lru_cache boundary.

Changes

Cohort / File(s) Summary
Port Normalization
cognee/infrastructure/databases/graph/get_graph_engine.py
Normalize graph_database_port by coercing "" and None to None in create_graph_engine before delegating to _create_graph_engine, avoiding distinct @lru_cache keys for empty string vs null.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Possibly related PRs

Suggested reviewers

  • Vasilije1990
🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Description check ⚠️ Warning The PR description clearly explains the problem, fix, and impact, but the required template sections (Acceptance Criteria, Type of Change, Screenshots, Pre-submission Checklist, DCO Affirmation) are not filled out. Complete the PR description template by filling out all required sections including Type of Change, Acceptance Criteria, Pre-submission Checklist, and DCO Affirmation to meet repository standards.
✅ Passed checks (2 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately describes the main change: coercing empty port strings to None in the create_graph_engine function, which is the primary objective of this PR.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ 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.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

🧹 Nitpick comments (1)
cognee/infrastructure/databases/graph/get_graph_engine.py (1)

98-100: Normalize graph_database_port before the cached call to avoid duplicate cache keys.

This normalization happens inside @lru_cache scope, so "" and None still create separate cache entries even though both map to None for adapters.

♻️ Proposed refactor
 def create_graph_engine(
@@
 ):
+    normalized_port = (
+        None
+        if graph_database_port is None
+        or (isinstance(graph_database_port, str) and graph_database_port.strip() == "")
+        else graph_database_port
+    )
     return _create_graph_engine(
         graph_database_provider,
         graph_file_path,
         graph_database_url,
         graph_database_name,
         graph_database_username,
         graph_database_password,
-        graph_database_port,
+        normalized_port,
         graph_database_key,
         graph_dataset_database_handler,
     )
@@
-        # Coerce empty/None port to None so adapters can apply their own defaults
-        port = graph_database_port if graph_database_port not in ("", None) else None
-
         return adapter(
             graph_database_url=graph_database_url,
             graph_database_username=graph_database_username,
             graph_database_password=graph_database_password,
-            graph_database_port=port,
+            graph_database_port=graph_database_port,
             graph_database_key=graph_database_key,
             database_name=graph_database_name,
         )
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@cognee/infrastructure/databases/graph/get_graph_engine.py` around lines 98 -
100, Normalize graph_database_port before the cached call to avoid duplicate
lru_cache keys: ensure the variable graph_database_port is coerced (treat "" and
None as None) prior to invoking the `@lru_cache-decorated` function or building
the cache key so that both "" and None map to the same port value; update the
code locations that compute or pass port (the port assignment currently using
graph_database_port if graph_database_port not in ("", None) else None) so
normalization occurs before the cached lookup in get_graph_engine (and any
helper that calls the cached adapter resolution).
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In `@cognee/infrastructure/databases/graph/get_graph_engine.py`:
- Around line 98-100: Normalize graph_database_port before the cached call to
avoid duplicate lru_cache keys: ensure the variable graph_database_port is
coerced (treat "" and None as None) prior to invoking the `@lru_cache-decorated`
function or building the cache key so that both "" and None map to the same port
value; update the code locations that compute or pass port (the port assignment
currently using graph_database_port if graph_database_port not in ("", None)
else None) so normalization occurs before the cached lookup in get_graph_engine
(and any helper that calls the cached adapter resolution).

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 84cf6562-bf2f-45ce-a386-e31606b2041b

📥 Commits

Reviewing files that changed from the base of the PR and between 5469622 and 650131b.

📒 Files selected for processing (1)
  • cognee/infrastructure/databases/graph/get_graph_engine.py

Address CodeRabbit feedback: normalize graph_database_port in the
uncached create_graph_engine() wrapper instead of inside the
@lru_cache-decorated _create_graph_engine(). This prevents "" and
None from creating separate cache entries.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant