Repository navigation
Description
What happened?
A @generative call with requirements= and a strategy= gives the caller no way to tell whether the returned value passed validation.
When the strategy exhausts its loop_budget, BaseSamplingStrategy._sample returns SamplingResult(success=False). Its result is whichever failed attempt select_from_failure picked. The stub then drops everything except the parsed value:
SyncGenerativeStub.__call__/AsyncGenerativeStub.__call__(mellea/stdlib/components/genstub.py) callact/aactwithoutreturn_sampling_results, so it defaults toFalse.aact(mellea/stdlib/functional.py) then returns only(sampling_result.result, ctx)and discardssampling_result.success.- The stub returns
response.parsed_repr.
@generative does not accept return_sampling_results, and nothing is raised on exhaustion. A failed answer and a validated answer are indistinguishable. The only signal is the INFO log line Invoking select_from_failure after N failed attempts.
This contradicts the documentation. how-to/enforce-structured-output.md (lines 285-286, "Choosing between the two patterns") says:
Both patterns support the full IVR loop, requirements, sampling strategies, and
SamplingResultinspection.
"Both patterns" are @generative and instruct(format=...) / act(format=...). SamplingResult inspection works for the second, not for @generative. The same sentence appears in the 0.7.0, 0.8.0 and current docs.
What did you expect to happen?
Not a silent failure. My preference would be a raised exception. A warnings.warn would be less disruptive to exiting code (but it may be code that is doing a different thing than what its author expects).
How can we reproduce it?
import os
from pydantic import BaseModel
from mellea import MelleaSession, generative, req
from mellea.backends.openai import OpenAIBackend
from mellea.stdlib.requirements import simple_validate
from mellea.stdlib.sampling import RejectionSamplingStrategy
def session() -> MelleaSession:
backend = OpenAIBackend(
base_url=os.environ["OPENAI_URL"],
api_key=os.environ["OPENAI_API_KEY"],
)
return MelleaSession(backend)
class Out(BaseModel):
n: int
@generative
def f(x: str) -> Out:
"""Return an integer n for the input."""
def never():
"""A requirement that always fails."""
return req(
"n must be 12345",
validation_fn=simple_validate(lambda s: (False, "n was not 12345")),
)
strategy = RejectionSamplingStrategy(loop_budget=2)
# 1. @generative: every attempt fails validation, but the caller gets a plain value.
r = f(session(), x="hi", requirements=[never()], strategy=strategy)
print("generative:", type(r).__name__, r)
# 2. instruct(): the same failure is observable via return_sampling_results.
sr = session().instruct(
"Reply with a number.",
requirements=[never()],
strategy=strategy,
return_sampling_results=True,
)
print("instruct: success =", sr.success, "| attempts =", len(sr.sample_generations))Output (log lines trimmed to the relevant ones):
FAILED. Valid: 0/1. Failed:
- n must be 12345
FAILED. Valid: 0/1. Failed:
- n must be 12345
Invoking select_from_failure after 2 failed attempts.
generative: Out n=12345
FAILED. Valid: 0/1. Failed:
- n must be 12345
FAILED. Valid: 0/1. Failed:
- n must be 12345
Invoking select_from_failure after 2 failed attempts.
instruct: success = False | attempts = 2
The @generative call returns Out(n=12345) after two failed validations, with no
exception and no warning. The instruct call reports success = False for the
same situation.
Mellea version
0.9.0Any environmental details we need to know?
macOS 26.7.2 arm64, Python 3.12.11, OpenAIBackend against an OpenAI-compatible server
Anything else we need to know?
N/A
Relevant log output
N/A
Activity
fix(genstub): warn when sampling strategy doesn't meet requirements