The ConfidenceAdapter is designed for classification tasks where the LLM returns structured JSON with enum-constrained fields. It uses token-level log-probabilities to distinguish between genuine understanding and 'lucky guesses'.
Prerequisites:
- The LLM must support token-level logprobs (e.g., OpenAI
gpt-4.1, gpt-4.1-mini or Google Gemini gemini-2.5-flash). - The
response_format must use enum constraints to allow the adapter to measure confidence over the allowed categories.
import gepa
from gepa.adapters.confidence_adapter import ConfidenceAdapter
adapter = ConfidenceAdapter(
model="openai/gpt-4.1-mini",
field_path="category_name",
response_format={
"type": "json_schema",
"json_schema": {
"name": "classification",
"strict": True,
"schema": {
"type": "object",
"properties": {
"category_name": {
"type": "string",
"enum": [
"Bills/Electricity",
"Bills/Gas & Oil",
"Food & Drinks/Restaurants",
"Shopping/Electronics",
"Shopping/Video Games",
],
}
},
"required": ["category_name"],
"additionalProperties": False,
},
},
},
)
result = gepa.optimize(
seed_candidate={"system_prompt": "Classify the following transaction."},
trainset=[
{"input": "UBER EATS payment", "answer": "Food & Drinks/Restaurants", "additional_context": {}},
{"input": "LIGHT electricity bill", "answer": "Bills/Electricity", "additional_context": {}},
{"input": "Steam purchase", "answer": "Shopping/Video Games", "additional_context": {}},
],
adapter=adapter,
reflection_lm="openai/gpt-4.1",
max_metric_calls=500,
)