Skip to content

feat(proxy-clients): pydantic-ai / mem0 / graphiti integration layer - #1202

Open
RainGodPRC wants to merge 2 commits into
TauricResearch:mainfrom
RainGodPRC:feat/proxy-clients-pydantic-ai-mem0-graphiti
Open

feat(proxy-clients): pydantic-ai / mem0 / graphiti integration layer#1202
RainGodPRC wants to merge 2 commits into
TauricResearch:mainfrom
RainGodPRC:feat/proxy-clients-pydantic-ai-mem0-graphiti

Conversation

@RainGodPRC

Copy link
Copy Markdown

What

Three complementary subsystems wired through a shared proxy-client layer. All opt-in via environment variables — zero regression when disabled (the existing LangGraph pipeline is unchanged unless TRADINGAGENTS_USE_PYDANTIC_AI=1 / TRADINGAGENTS_MEM0_ENABLED=1 are set).

Shared layer (tradingagents/llm_clients/proxy_clients.py)

  • resolve_gateway_token() — extracts the cc-switch Bearer token from env or a running Claude Code process (the proxy at 127.0.0.1:15721 rejects x-api-key, requires Authorization: Bearer)
  • make_anthropic_client() — auth_token-based (Bearer, not x-api-key)
  • Local HuggingFace embedder — the proxy serves no /v1/embeddings; mem0 + graphiti both use this local fallback
  • Factory helpers: make_pydantic_ai_agent(), make_mem0_memory(), make_graphiti()

pydantic-ai backend (structured.py)

Optional path in invoke_structured_or_freetext, enabled by TRADINGAGENTS_USE_PYDANTIC_AI=1. Adds a validation-retry loop (the model is prompted to correct malformed output) that the legacy one-shot fallback lacked — preserves structured signals (BUY/SELL/HOLD, rating) that currently get lost when structured output fails and silently degrades to free text.

Trader, Portfolio Manager, Research Manager pass schema= + instructions= so they benefit when the flag is on.

mem0 decision memory (mem0_bridge.py + trading_graph._run_graph)

Mem0Bridge provides remember_decision() / recall_relevant(). Wired into _run_graph: semantic recall of past decisions is appended to past_context before the pipeline runs; the completed decision is stored after. Cross-ticker recall ("I've seen this Fed-pivot pattern before") complements the ticker-matched markdown log. Enabled by TRADINGAGENTS_MEM0_ENABLED=1.

graphiti Winterfell import (scripts/import_winterfell_to_graphiti.py)

Imports Decisions/Collisions/Analysis/Insights Markdown as temporal episodes, using frontmatter date/created as reference_time. Monkey-patches graphiti's search functions to skip Kuzu's broken fulltext/vector indices (Kuzu is deprecated in graphiti for this reason) — ingestion works on Kuzu, but full dedup/temporal invalidation requires Neo4j/FalkorDB.

Tests

  • 3 smoke tests (pydantic-ai, mem0, graphiti) — verify proxy connectivity
  • test_trader_pydantic_ai_integration.py — Trader node produces structured proposal via pydantic-ai
  • test_mem0_bridge_integration.py — cross-ticker semantic recall
  • test_e2e_integration.py — full loop: graphiti ingest → pydantic-ai decision → mem0 recall

Verification

All tests pass against the local cc-switch proxy. No regression in the existing 492-test suite (imports verified, flags default off).

Known limitations

  • graphiti on Kuzu has no temporal invalidation (fulltext index bug); full capability needs Neo4j/FalkorDB
  • Token extraction depends on a running Claude Code process (ccs- gateway token)
  • trading_graph.py also carries a pre-existing _fetch_returns signature refinement that was in the working tree; it's logically intertwined with the mem0 bridge in _run_graph

🤖 Generated with Claude Code

@gemini-code-assist

Copy link
Copy Markdown

Caution

The consumer version of Gemini Code Assist on GitHub has been sunset. All code review activity has officially ceased.

Three complementary subsystems wired through a shared proxy-client layer,
all opt-in via environment variables (zero regression when disabled).

Shared layer (tradingagents/llm_clients/proxy_clients.py):
- resolve_gateway_token() extracts the cc-switch Bearer token from env
  or a running Claude Code process (the proxy rejects x-api-key)
- make_anthropic_client() with auth_token (Bearer, not x-api-key)
- Local HuggingFace embedder (proxy serves no /v1/embeddings)
- Factory helpers for pydantic-ai Agent, mem0 Memory, graphiti Graphiti

pydantic-ai backend (structured.py):
- Optional path in invoke_structured_or_freetext, enabled by
  TRADINGAGENTS_USE_PYDANTIC_AI=1, with a validation-retry loop that
  the legacy one-shot fallback lacked — preserves BUY/SELL/HOLD signals
- Trader, Portfolio Manager, Research Manager pass schema+instructions

mem0 decision memory (mem0_bridge.py + trading_graph._run_graph):
- remember_decision() / recall_relevant() back the Portfolio Manager
- Semantic cross-ticker recall complements the ticker-matched markdown log
- Enabled by TRADINGAGENTS_MEM0_ENABLED=1

graphiti Winterfell import (scripts/import_winterfell_to_graphiti.py):
- Imports Decisions/Collisions/Analysis/Insights as temporal episodes
- reference_time from frontmatter date/created
- Monkey-patches graphiti search to skip Kuzu fulltext/vector index bugs
  (Kuzu backend is deprecated in graphiti for this reason)

Tests: three smoke tests + mem0 bridge integration + trader pydantic-ai
integration + end-to-end (graphiti ingest -> pydantic-ai decision -> mem0 recall).

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@RainGodPRC
RainGodPRC force-pushed the feat/proxy-clients-pydantic-ai-mem0-graphiti branch from 68700b3 to 573da75 Compare August 8, 2026 09:30
Wrap each provider's sync _generate / async _agenerate with tenacity
exponential backoff so a saturated inference gateway (e.g. local proxy at
127.0.0.1:15721 returning HTTP 529) no longer exhausts the SDK's built-in
retries and aborts the whole run with 'Service was busy'.

Only transient errors (429 / 5xx / 529 / timeouts / connection drops) are
retried; auth, validation, and not-found errors fail fast. Retry budget is
tunable via TRADINGAGENTS_LLM_MAX_ATTEMPTS / TRADINGAGENTS_LLM_RETRY_MAX_WAIT.

Co-Authored-By: WorkBuddy <workbuddy@anthropic.com>
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