feat(history): add typed history frames - #51
Conversation
2f13e38 to
e711cf7
Compare
Greptile SummaryThis PR introduces a normalized LM type system (
Confidence Score: 3/5The new HistoryFrame-based append API and the _history_to_lm_messages renderer are structurally misaligned, causing incorrect message layout for any history built with the new helpers. The core compatibility issue between HistoryFrame objects and the dict-based adapter rendering methods remains unresolved (noted in prior review threads). Additionally, _history_to_lm_messages generates a user+assistant pair for every frame, but append_inputs / append_outputs create separate single-purpose frames, so the renderer produces doubled, partially empty message pairs for any history built with the new API. Both defects affect the primary new feature surface. The large new files (core/types.py, clients/openai_format.py) and the adapter pipeline refactor are otherwise well-structured and appear safe. dspy/adapters/base.py (_history_to_lm_messages) and dspy/adapters/types/history.py (frame-level rendering contract) Important Files Changed
Sequence DiagramsequenceDiagram
participant Caller
participant Adapter
participant PlanFields as plan_fields
participant HistoryHelper as _history_to_lm_messages
participant Format as format()
participant ApplyPlan as _apply_planned_messages
participant LM
Caller->>Adapter: __call__(lm, lm_kwargs, signature, inputs)
Adapter->>PlanFields: plan_fields(lm, lm_kwargs, signature, inputs)
PlanFields->>PlanFields: detect History/Image/Audio/File fields
PlanFields->>HistoryHelper: _history_to_lm_messages(signature, history)
HistoryHelper-->>PlanFields: list[LMMessage] (history turns)
PlanFields->>PlanFields: remove history/media from signature and inputs
PlanFields-->>Adapter: plan dict (prompt_signature, messages, user_parts, tools, lm_kwargs)
Adapter->>Format: format(prompt_signature, demos, plan_inputs)
Format-->>Adapter: list[LMMessage] (system + demos + current user)
Adapter->>ApplyPlan: _apply_planned_messages(messages, plan)
ApplyPlan->>ApplyPlan: insert history messages before last user msg
ApplyPlan->>ApplyPlan: extend last user msg parts with media parts
ApplyPlan-->>Adapter: list[LMMessage] (complete context)
Adapter->>LM: call_lm(lm, LMRequest)
LM-->>Adapter: LMResponse
Adapter->>Adapter: parse_response(plan, response, lm)
Adapter-->>Caller: list[dict] (parsed output fields)
|
e711cf7 to
df0f0c3
Compare
df0f0c3 to
e22f99f
Compare
e22f99f to
5d3fbcf
Compare
Summary
HistoryFrameandObservationmodels while preserving legacymessages=constructionStack
isaac/react-v2-base-normalized-adapter(62adc7d34)Validation
uv run --extra dev pytest -q tests/adapters/test_history.pyuv run --extra dev pytest -q tests/adapters/test_history.py tests/adapters/test_history_formatting.py tests/adapters/test_history_lm_messages.py tests/adapters/test_tool.py tests/predict/test_reactv2.py tests/adapters/test_chat_adapter.py::test_chat_adapter_with_tool tests/adapters/test_chat_adapter.py::test_tool_call_with_null_content_does_not_raise