Skip to content

Commit

Permalink
lf.QueryInvocation.output to return lf.MappingError when OOP failed.
Browse files Browse the repository at this point in the history
Previously accessing `output` for bad LLM response will raise.

PiperOrigin-RevId: 708050827
  • Loading branch information
daiyip authored and langfun authors committed Dec 19, 2024
1 parent f4dcfd1 commit 74ee960
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
11 changes: 10 additions & 1 deletion langfun/core/structured/querying.py
Original file line number Diff line number Diff line change
Expand Up @@ -583,7 +583,16 @@ def lm_request(self) -> lf.Message:

@functools.cached_property
def output(self) -> Any:
return query_output(self.lm_response, self.schema)
"""The output of `lf.query`. If it failed, returns the `MappingError`."""
try:
return query_output(self.lm_response, self.schema)
except mapping.MappingError as e:
return e

@property
def has_error(self) -> bool:
"""Returns True if the query failed to generate a valid output."""
return isinstance(self.output, BaseException)

@property
def elapse(self) -> float:
Expand Down
10 changes: 10 additions & 0 deletions langfun/core/structured/querying_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -1051,6 +1051,16 @@ def test_query(self):

class QueryInvocationTest(unittest.TestCase):

def test_basics(self):
lm = fake.StaticSequence([
'Activity(description="hi"',
])
with querying.track_queries() as queries:
querying.query('foo', Activity, default=None, lm=lm)

self.assertTrue(queries[0].has_error)
self.assertIsInstance(queries[0].output, mapping.MappingError)

def test_to_html(self):
lm = fake.StaticSequence([
'Activity(description="hi")',
Expand Down

0 comments on commit 74ee960

Please sign in to comment.