LangChainAdapter¶
gepa.adapters.langchain_adapter.langchain_adapter.LangChainAdapter(rollout_fn: RolloutFn, eval_fn: EvalFn, num_threads: int = 32, custom_proposer: ProposalFn | None = None, reflective_record_fn: ReflectiveRecordFn = _default_reflective_record, show_progress: bool = True)
¶
Bases: GEPAAdapter
GEPA adapter for arbitrary LangChain rollouts.
Caller supplies
rollout_fn(candidate, example) -> state dict: any LangChain pipeline — a single chat-model invocation, an agent built withcreate_agent, a custom LangGraph graph, RAG, etc. Must return a state dict; for single-turn cases return a dict with the messages key{"messages": messages + [AIMessage("llm response")]}. For agents, return the full agent state directly (e.g.agent.invoke(...)).eval_fn(example, state) -> (score, feedback): scores the rollout state. Uselast_message_text(state)if you only need the final assistant text; agents can inspect tool calls instate["messages"]directly. Note: ifrollout_fnraises, the adapter substitutes a stand-in state of the form{"messages": [AIMessage("ERROR: <type>: <msg>")], "error": e}and still callseval_fnwith it. Checkstate.get("error")to detect rollout failures and score them appropriately (e.g. return 0.0 with a feedback string explaining the failure to the reflection LM).reflective_record_fn(example, state, score, feedback) -> mapping(optional): builds the per-example record passed to the reflection LM. Defaults to{"Inputs", "Generated Outputs", "Feedback"}derived fromexample["input"]andlast_message_text(state). Override to surface tool-call traces, intermediate steps, or domain-specific context.num_threads: parallelism forevaluate(default 32).custom_proposer: optionalProposalFnto override GEPA's default text-proposal behavior.show_progress: whether to render a tqdm progress bar duringevaluate(default True).
The candidate is a dict[str, str] of named text components; rollout_fn
decides how those components are wired into the call.
Source code in gepa/adapters/langchain_adapter/langchain_adapter.py
Attributes¶
rollout_fn = rollout_fn
instance-attribute
¶
eval_fn = eval_fn
instance-attribute
¶
num_threads = num_threads
instance-attribute
¶
reflective_record_fn = reflective_record_fn
instance-attribute
¶
propose_new_texts = custom_proposer
instance-attribute
¶
show_progress = show_progress
instance-attribute
¶
Functions¶
evaluate(batch: list[DataInst], candidate: dict[str, str], capture_traces: bool = False) -> EvaluationBatch
¶
Source code in gepa/adapters/langchain_adapter/langchain_adapter.py
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 | |