feat: implements base optimization logic in the optimization sdk#116
feat: implements base optimization logic in the optimization sdk#116andrewklatzke wants to merge 6 commits intomainfrom
Conversation
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 3 potential issues.
Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, have a team admin enable autofix in the Cursor dashboard.
| start_idx = response_str.find('{', start_idx + 1) | ||
| if start_idx == -1: | ||
| break | ||
| brace_count = 0 |
There was a problem hiding this comment.
Balanced-brace scanner retry uses stale start index
Medium Severity
The balanced-brace scanning fallback in extract_json_from_response has broken retry logic. When a balanced {…} block fails json.loads, start_idx is updated to the next { after the original start (which is before i), and brace_count is reset to 0, but the for loop continues from i + 1 — already past the new start_idx. This means the scanner never re-processes characters from the updated start position. On the next time brace_count hits 0, response_str[start_idx:i + 1] spans from the stale inner start_idx to the new closing brace, producing a garbage substring that won't parse. The retry path effectively never works.
| }, | ||
| "required": ["passed", "rationale"], | ||
| }, | ||
| ) |
There was a problem hiding this comment.
| if self.judges is None and self.on_turn is None: | ||
| raise ValueError("Either judges or on_turn must be provided") | ||
| if self.judge_model is None: | ||
| raise ValueError("judge_model must be provided") |
There was a problem hiding this comment.
Missing validation for empty variable_choices list
Medium Severity
__post_init__ validates that context_choices and model_choices each have at least one element, but no equivalent check exists for variable_choices. An empty list passes validation but causes random.choice() to raise an IndexError at runtime in _run_optimization and _create_optimization_context.


Requirements
Related issues
Pulls in the optimize method from the moonshot branch, updates it to be more production-ready, adds tests, logically splits up code into more manageable chunks.
Describe the solution you've provided
This is the initial implementation of the optimization method that we're pulling into this SDK. Right now this services the same surface area as the moonshot - namely, it implements the optimize_from_options() method while leaving the optimize_from_config() method unimplemented. This also does not handle additional features we'll be adding, such as the ability to compare to ground-truth responses or the ability to post back to LaunchDarkly.
The logs are set to debug level, so enabling them will allow you to trace along with the progress.
Using the manual/passing options implementation of this looks like:
Note
Medium Risk
Introduces a large amount of new orchestration logic around LLM prompting, structured JSON parsing, and iterative control flow; errors or edge cases could cause incorrect scoring loops or brittle parsing despite added tests.
Overview
Implements a real
OptimizationClientto iteratively optimize an agent viaoptimize_from_options, executing agent turns, scoring responses with either LD-configured judges (judge_key) or inline acceptance-statement judges, and generating new configuration variations until thresholds pass ormax_attemptsis reached.Adds first-class types (
OptimizationOptions,OptimizationContext,OptimizationJudge,AIJudgeCallConfig,ToolDefinition) plus utilities for structured tool-based outputs (evaluation + variation tools), JSON extraction from LLM responses, and runtime interpolation of{{variables}}while preserving raw instruction templates.Re-exports the new API from
__init__.py, updates smoke tests accordingly, and adds extensive unit tests covering judge evaluation, variation prompting/application, and full optimization loop behavior.Written by Cursor Bugbot for commit f8e5509. This will update automatically on new commits. Configure here.