You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
fix: resolve Python FFI buffer limits and single-quote escaping in tool calling #760
While working with the Cactus codebase, I encountered three related bugs/limitations in the FFI bindings and tool call parsing logic:
1. Invalid JSON generated during single-quoted parameter parsing in tool calls
LiquidAI models (LFM2/LFM2.5) format tool calls as Python-like arguments (e.g., my_tool(param='val')).
The issue: In cactus-engine/src/chat_tools.h, the parser function py_literal_to_json translates escaped single quotes (\') literally as \' inside the resulting double-quoted JSON string (producing "hello \'world\"").
Why it fails: Per RFC 8259, \' is an invalid escape sequence in JSON. Standard JSON parsers (e.g. C++ picojson and python's json module) fail with a syntax error, causing tool execution to abort.
Proposed fix: Modify the escape sequence parser to unescape \' to ' in the double-quoted JSON string since single quotes do not require escaping.
2. RuntimeError when retrieving documents > 4KB from index
The issue: The vector database supports indexing documents up to 65,535 bytes (Index::validate_documents). However, the Python FFI binding cactus_index_get uses a hardcoded buffer limit _INDEX_DOC_BUF_SIZE = 4096.
Why it fails: When retrieving a document that exceeds 4KB, the C++ code returns -1 (buffer too small), causing the Python wrapper to raise RuntimeError: Failed to get from index.
Proposed fix: Bump _INDEX_DOC_BUF_SIZE to 65536 in the Python FFI bindings to accommodate the maximum supported document size.
The issue with the py_literal_to_json function not handling escaped single quotes correctly in JSON seems like a key point to inspect. That could be causing significant parsing errors.
Activity
Kaustubh1235 commentedon Sep 19, 2026
The issue with the
py_literal_to_jsonfunction not handling escaped single quotes correctly in JSON seems like a key point to inspect. That could be causing significant parsing errors.