Skip to content

bug: parsed_repr has runtime/type mismatch for Instruction when format= is set #1313

Closed as not planned

Description

@planetf1

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

  1. added
    bug
    on Jun 22, 2026
  2. added a commit that references this issue on Jun 22, 2026
  3. added
    p2
    on Jun 23, 2026
  4. self-assigned this
    on Jul 21, 2026
  5. planetf1 commented on Jul 21, 2026

    @planetf1
    ContributorAuthor

    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 .parsed property. Neither exists on main — #1284 was closed unmerged, and #1273/#1274 shipped as a docs-only fix instead (#1333). Today, nothing narrows act()/instruct()'s return type based on format=, so the specific failure described here (statically MyModel, actually str, silent AttributeError) can't occur.

    What's actually true on main: format= has no effect on output parsing at all. Every backend threads _format into post_processing() purely for logging; nothing calls model_validate_json on the result. result.value and result.parsed_repr are always the raw JSON string regardless of format=. That's a narrower (and arguably more surprising) gap than the one this issue tracks — one side effect is that docs/docs/how-to/act-and-aact.md currently shows a format= example claiming result.value is "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 in Component.parse() plus threading a _format attribute through the backend call sites would get runtime correctness without touching any _parse implementation. But weighed against the cost, it's not worth doing right now:

    • This is 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 every act/instruct call, 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 reading parsed_repr under format=.
    • Matching this in the static types would roughly double the ~24 existing act/instruct/aact/ainstruct overloads across session.py and functional.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.

  6. planetf1 commented on Jul 21, 2026

    @planetf1
    ContributorAuthor

    Correction to my comment above: the docs/docs/how-to/act-and-aact.md doc 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 shows result.value as a JSON string, with a callout on the cast(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/.parsed narrowing exist on main, format= is decode-only) still stands independently of this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bugp2

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions