Repository navigation
Expand file tree
/
Copy pathgcf_encoder.py
More file actions
64 lines (48 loc) · 1.76 KB
/
Copy pathgcf_encoder.py
File metadata and controls
64 lines (48 loc) · 1.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
"""
Optional GCF (Graph Compact Format) encoding for tool responses.
When enabled via CGC_OUTPUT_FORMAT=gcf, tool results are encoded using GCF
instead of JSON, reducing token consumption by ~62% on typical code graph data.
GCF encodes structured data with positional fields: keys declared once in a
header, values pipe-delimited per row. LLMs read it natively with 100%
comprehension accuracy across all frontier models tested.
Requires: pip install gcf-python
Falls back to JSON silently if the package is not installed.
See https://gcformat.com for the full specification.
"""
import json
import logging
import os
from typing import Any
logger = logging.getLogger(__name__)
_gcf_encode = None
_gcf_checked = False
def _load_gcf():
"""Lazily load GCF encoder. Check once, cache the result."""
global _gcf_encode, _gcf_checked
if _gcf_checked:
return _gcf_encode
_gcf_checked = True
try:
from gcf import encode_generic
_gcf_encode = encode_generic
except ImportError:
_gcf_encode = None
return _gcf_encode
def is_gcf_enabled() -> bool:
"""Check if GCF output format is enabled via environment variable."""
return os.environ.get("CGC_OUTPUT_FORMAT", "").lower() == "gcf"
def encode_response(result: Any) -> str:
"""Encode a tool result as GCF (if enabled and available) or JSON.
Args:
result: The tool result dictionary to encode.
Returns:
Encoded string (GCF or JSON with indent=2).
"""
if is_gcf_enabled():
encoder = _load_gcf()
if encoder is not None:
try:
return encoder(result)
except Exception:
logger.debug("GCF encoding failed; falling back to JSON", exc_info=True)
return json.dumps(result, indent=2)