Repository navigation
Description
Problem
When `format=` is passed to `act()` or `instruct()`, the `format=` overloads (PR #1284) narrow the return type to `ComputedModelOutputThunk[MyModel]`. That narrowing is reflected on `parsed_repr: S | None` — so `result.parsed_repr` is statically typed `MyModel | None`. However, at runtime it is always a plain `str`.
The root cause is `Instruction._parse` (mellea/stdlib/components/instruction.py:235), which ignores `_format` and always returns the raw string value:
```python
def _parse(self, computed: ModelOutputThunk) -> str:
return computed.value if computed.value is not None else ""
```
So this type-checks but raises `AttributeError` at runtime:
```python
result = session.act(Instruction("Classify this"), format=Sentiment)
result.parsed_repr.label # typed Sentiment | None — AttributeErrors at runtime
```
Workaround
Use `.parsed` instead of `.parsed_repr`. `.parsed` was added in PR #1284 specifically to provide a runtime-correct path — it calls `model_validate_json` directly and returns the Pydantic instance:
```python
result.parsed.label # typed Sentiment | None — correct at runtime
```
Why this was not fixed in PR #1284
The proper fix requires `Instruction` to become generic over its output type (`Instruction[S]`) so that `_parse` can be typed `-> S` where `S` is either `str` (no `format=`) or `BaseModelSubclass` (with `format=`). That is a non-trivial change to the component hierarchy:
- `Instruction` is currently `Component[str]`; making it `Component[S]` requires propagating `S` through all construction sites
- `format=` is passed at the `act()` call site, after `Instruction` is constructed — so the type parameter cannot be inferred from the action alone
- There are 22 `_parse` implementations across the component hierarchy; a principled fix should address the pattern consistently, not just patch `Instruction`
This scope belongs in its own PR rather than extending #1284 further.
Acceptance criteria
- `result.parsed_repr` delivers a `MyModel` instance (not a `str`) at runtime when `format=MyModel` was passed to `act()`
- The static type `S | None` matches the runtime value — no hidden `AttributeError` risk
- `.parsed` and `parsed_repr` are consistent for the `Instruction` path (they may remain separate entry points but should agree on the value)
Activity
docs(types): reference generative-computing#1313 for parsed_repr runt…
planetf1 commentedon Jul 21, 2026
Closing as not-planned after checking this against current
main.The premise no longer holds. This issue describes a mismatch that depends on PR #1284's return-type overloads and its
.parsedproperty. Neither exists onmain— #1284 was closed unmerged, and #1273/#1274 shipped as a docs-only fix instead (#1333). Today, nothing narrowsact()/instruct()'s return type based onformat=, so the specific failure described here (staticallyMyModel, actuallystr, silentAttributeError) can't occur.What's actually true on
main:format=has no effect on output parsing at all. Every backend threads_formatintopost_processing()purely for logging; nothing callsmodel_validate_jsonon the result.result.valueandresult.parsed_reprare always the raw JSON string regardless offormat=. That's a narrower (and arguably more surprising) gap than the one this issue tracks — one side effect is thatdocs/docs/how-to/act-and-aact.mdcurrently shows aformat=example claimingresult.valueis "A Planet instance", which is false. Fixing that doc line separately.Why not take on the full fix instead of closing: a real fix is buildable without the
Instruction[S]generics refactor this issue worried about — centralising inComponent.parse()plus threading a_formatattribute through the backend call sites would get runtime correctness without touching any_parseimplementation. But weighed against the cost, it's not worth doing right now:p2/niche with no reported case of anyone hitting it in practice, and the harder version of this fix was already declined once (feat(thunk+types): add .parsed property and wire format= overloads for cast-free structured output #1284 closed for a docs-only alternative).Component.parse()is a chokepoint hit by everyact/instructcall, every sampling strategy, and batch generation — bypassing per-component parsing there when a format is set is a real behaviour change, not a no-op fix, for anyone currently readingparsed_reprunderformat=.act/instruct/aact/ainstructoverloads acrosssession.pyandfunctional.py— a large, repetitive diff for a compile-time nicety on a feature nobody's asked for yet.Happy to reopen if someone hits this in practice and wants to make the case for taking on that cost.
planetf1 commentedon Jul 21, 2026
Correction to my comment above: the
docs/docs/how-to/act-and-aact.mddoc bug I mentioned no longer exists — it was already fixed by #1333 (commit 70d9f5f, "docs: clarify format= value contract and model_validate_json pattern"). The current doc correctly showsresult.valueas a JSON string, with a callout on thecast(Planet, result.value)pitfall and a pointer to@generative. I was working from a stale read of the file. No follow-up doc PR needed; the rest of the closing rationale (no overloads/.parsednarrowing exist onmain,format=is decode-only) still stands independently of this.