Skip to content

Bug: format_type passed through resolver_params crashes lx.extract with AttributeError #542

Description

@Om-singhaI

Describe the overall issue and situation

Passing format_type through resolver_params makes lx.extract crash with AttributeError: 'str' object has no attribute 'value' before the model is ever called. It isn't just YAML. FormatType.JSON fails the same way, and so do the strings "yaml" and "json". The key is still supported until v2.0.0 (FormatHandler.from_resolver_params maps it and emits a DeprecationWarning), so older code that sets it breaks.

from_resolver_params unwraps the enum to its string (val = val.value) and passes strings through as is, so the handler holds "yaml" instead of FormatType.YAML. _add_fences then calls .value on that str. With fence_output=False the prompt doesn't crash, but the examples render as JSON even though YAML was asked for, and parsing the YAML answer raises the same error. suppress_parse_errors doesn't catch it. This goes back to #239 (v1.1.0).

Steps to reproduce the issue

On main (7129597), Python 3.13, PyYAML 6.0.3. The fake model returns a fixed YAML answer, so nothing goes over the network.

import langextract as lx
from langextract.core import base_model
from langextract.core import types


class FakeModel(base_model.BaseLanguageModel):

  def infer(self, batch_prompts, **kwargs):
    for _ in batch_prompts:
      output = "```yaml\nextractions:\n- person: Alice\n```"
      yield [types.ScoredOutput(score=1.0, output=output)]


examples = [
    lx.data.ExampleData(
        text="Bob met Carol.",
        extractions=[lx.data.Extraction("person", "Bob")],
    )
]

result = lx.extract(
    "Alice went home.",
    prompt_description="Extract people.",
    examples=examples,
    model=FakeModel(),
    resolver_params={"format_type": lx.data.FormatType.YAML},
    show_progress=False,
)
print([(e.extraction_class, e.extraction_text) for e in result.extractions])

Actual behavior

What I get:

  File ".../langextract/core/format_handler.py", line 149, in format_extraction_example
    return self._add_fences(formatted) if self.use_fences else formatted
  File ".../langextract/core/format_handler.py", line 249, in _add_fences
    fence_type = self.format_type.value
AttributeError: 'str' object has no attribute 'value'

Same traceback with FormatType.JSON and a JSON answer.

Expected behavior

[('person', 'Alice')], which is what I get when I pass format_type=lx.data.FormatType.YAML to lx.extract directly.

Any additional content

Converting the value to FormatType in from_resolver_params fixes it. I have that ready with tests if the approach works for you. In my version an unknown value like "xml" raises ValueError. from_kwargs maps anything that isn't "json" to YAML instead, so tell me if you'd rather match that.

A plain string at the top level, lx.extract(..., format_type="yaml"), crashes the same way. I left that out since that value is also passed to the provider when you use model_id.

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions