Skip to content

Fix: strip markdown code fences from Cypher query in DatabaseAgent - #332

Merged
VladSam42 merged 2 commits into
biocypher:stagingfrom
rupshali21999:fix/strip-markdown-fences-from-cypher-query
Jun 26, 2026
Merged

VladSam42 merged 2 commits into
biocypher:stagingfrom
rupshali21999:fix/strip-markdown-fences-from-cypher-query

Conversation

@rupshali21999

Copy link
Copy Markdown
Collaborator

Problem: BioCypherPromptEngine.generate_query() sometimes returns Cypher wrapped in markdown code fences (```cypher). When DatabaseAgent._generate_query() passes this directly to driver.query(), Neo4j raises a CypherSyntaxError because it cannot parse the backticks.

Fix: Strip markdown code fences and any non-Cypher preamble from the generated query before passing it to Neo4j.

@slobentanzer

Copy link
Copy Markdown
Contributor

@rupshali21999 thanks a lot for the PR! When adding new functionality, it is imperative to test it if the behaviour that should be fixed is not already tested for. This is highly likely, because otherwise some tests would have already failed in the current CI suite.

Could you please add test coverage for this string sanitisation behaviour? One way to think about it is to add tests that will fail in the state of the code before your introduced change. Some people work by adding these tests first, and then the implementation that fixes them (test-driven development). I do like that approach as well and try to do it as often as possible.

Further, can you think of a generalisation of the tests and implementation, to be applicable beyond the Cypher scope; for instance, any query language? The prompt generation accepts other languages (e.g., Postgres / SQL dialects).

@rupshali21999

Copy link
Copy Markdown
Collaborator Author

@slobentanzer I've updated the PR with:

Generalised the fix to work for any query language (removed Cypher-specific keyword detection)
Added 4 tests covering: markdown fences with Cypher, markdown fences with SQL, no language tag, and clean query passing through unchanged

@slobentanzer

Copy link
Copy Markdown
Contributor

@rupshali21999 please change the target of this PR to staging as well (same as #334)

@rupshali21999
rupshali21999 requested a review from fengsh27 June 24, 2026 20:34
@rupshali21999
rupshali21999 changed the base branch from main to staging June 24, 2026 20:37
@VladSam42
VladSam42 merged commit 2672685 into biocypher:staging Jun 26, 2026
1 check passed

This branch was successfully deployed

1 active deployment
Test CI — 9cf680f6 Deployed Jun 8, 2026 by rupshali21999 via test #651
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.

3 participants