Skip to content

fix: resolve Python FFI buffer limits and single-quote escaping in to…#761

Open
ApurveKaranwal wants to merge 1 commit into
cactus-compute:mainfrom
ApurveKaranwal:fix/ffi-buffer-and-tool-quotes
Open

fix: resolve Python FFI buffer limits and single-quote escaping in to…#761
ApurveKaranwal wants to merge 1 commit into
cactus-compute:mainfrom
ApurveKaranwal:fix/ffi-buffer-and-tool-quotes

Conversation

@ApurveKaranwal

Copy link
Copy Markdown

Description

This Pull Request addresses three issues identified within the Python FFI bindings and tool-calling parameter parsing logic:

  1. JSON Validation for Single-Quoted Parameter Parsing (chat_tools.h): LiquidAI models (LFM2/LFM2.5) format tool calls as Python-like arguments (e.g., my_tool(param='val')). If a parameter value contains an escaped single quote (\'), the parser function py_literal_to_json translated it literally as \' inside the resulting double-quoted JSON string (producing "hello \'world\""). Per RFC 8259, this is an invalid escape sequence. As a result, standard JSON parsers (e.g. C++ picojson and python's json module) threw syntax errors, aborting tool execution. This has been resolved by unescaping \' to ' in the double-quoted JSON output.

  2. FFI Document Retrieval Buffer Limit (cactus.py): The vector database supports indexing documents up to 65,535 bytes (Index::validate_documents). However, the Python FFI binding cactus_index_get used a hardcoded buffer limit _INDEX_DOC_BUF_SIZE = 4096. Attempting to retrieve documents exceeding 4KB caused the FFI to return -1, raising a RuntimeError on the Python side. The buffer limit has been increased to 65536 to safely accommodate the database's actual limit.

  3. RAG Query Buffer Limit (cactus.py): Increased the query results buffer size in cactus_rag_query from 64KB to 1MB to safely support RAG queries that return multiple large snippets.


Verification

  • Created a C++ unit test suite at cactus-engine/tests/test_chat_tools.cpp to verify that Python-like literals and LFM2 tool calls containing apostrophes are extracted correctly and parsed successfully by picojson.
  • Verified that the test suite compiles and passes successfully.
    Please let me know if you would like any modifications or additions.

Fixes #760

…ol calling

Signed-off-by: ApurveKaranwal <apurvekaranwal282@gmail.com>
@ApurveKaranwal
ApurveKaranwal force-pushed the fix/ffi-buffer-and-tool-quotes branch from 023282e to a967ab6 Compare July 11, 2026 08:11
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.

fix: resolve Python FFI buffer limits and single-quote escaping in tool calling

1 participant