evaluators icon indicating copy to clipboard operation
evaluators copied to clipboard

feat(python-sdk): add LLMGeneratorProtocol for framework-agnostic model injection

Open adnanrhussain opened this issue 5 months ago • 0 comments

Summary

Introduces a framework-agnostic model injection interface to the Python SDK, enabling evaluation frameworks (Inspect AI, Arize, Langfuse, Braintrust) to provide their own LLM backend without the SDK depending on any of them directly.

New types in sdks/python:

  • LLMGeneratorProtocol (typing.Protocol) — structural interface: one async generate(*, system, human, config) -> LLMResponse method
  • LLMResponse (NamedTuple) — structured response aligned with OTel GenAI semconv: content, model, input_tokens, output_tokens
  • GenerateConfig (dataclass) — temperature and max_tokens passthrough

BaseEvaluator changes:

  • Accepts optional llm_provider: LLMGeneratorProtocol in __init__
  • When set: formats the LangChain template to extract system/human strings, delegates the LLM call to the injected provider, parses JSON response via Pydantic directly
  • When not set: existing LangChain path, unchanged — fully backward compatible

Other:

  • _strip_json_fences now uses JSONDecoder.raw_decode — correctly handles trailing prose and multiple JSON objects in LLM responses
  • Root .gitignore updated to cover *.egg-info/, dist/, build/, logs/

Test plan

  • [x] 283 existing SDK tests pass (LangChain path unchanged)
  • [x] 11 new tests in tests/schemas/test_llm_provider.py cover protocol conformance, LLMResponse field defaults, GenerateConfig passthrough
  • [x] _strip_json_fences handles fenced, prose-wrapped, and trailing-prose responses

Integration packages

This PR is the base for #integrations PR which adds Inspect AI, Arize, Langfuse, and Braintrust adapters on top of this protocol.

🤖 Generated with Claude Code

adnanrhussain avatar Jun 11 '26 22:06 adnanrhussain