Invoke a function and return its result. This includes getting the call context from the API and deserializing data.
(self)
| 271 | return await mod.id() |
| 272 | |
| 273 | async def invoke(self) -> str: |
| 274 | """Invoke a function and return its result. |
| 275 | |
| 276 | This includes getting the call context from the API and deserializing data. |
| 277 | """ |
| 278 | fn_call = dag.current_function_call() |
| 279 | parent_name = await fn_call.parent_name() |
| 280 | |
| 281 | if not parent_name: |
| 282 | msg = ( |
| 283 | "Seems like the SDK module isn't registering the types correctly. " |
| 284 | "This is a bug." |
| 285 | ) |
| 286 | raise RegistrationError(msg) |
| 287 | |
| 288 | name = await fn_call.name() |
| 289 | parent_json = await fn_call.parent() |
| 290 | input_args = await fn_call.input_args() |
| 291 | |
| 292 | parent_state: dict[str, Any] = {} |
| 293 | if parent_json.strip(): |
| 294 | try: |
| 295 | parent_state = json.loads(parent_json) or {} |
| 296 | except ValueError as e: |
| 297 | logger.exception("Failed to decode JSON parent value") |
| 298 | msg = "Unable to decode the parent object's state" |
| 299 | extra = { |
| 300 | "parent_json": parent_json, |
| 301 | } |
| 302 | raise InvalidInputError(msg, extra=extra) from e |
| 303 | |
| 304 | inputs = {} |
| 305 | for arg in input_args: |
| 306 | # NB: These are already loaded by `input_args`, |
| 307 | # the await just returns the cached value. |
| 308 | arg_name = await arg.name() |
| 309 | arg_value = await arg.value() |
| 310 | try: |
| 311 | # Cattrs can decode JSON strings but use `json` directly |
| 312 | # for more granular control over the error. |
| 313 | inputs[arg_name] = json.loads(arg_value) |
| 314 | except ValueError as e: |
| 315 | logger.exception("Failed to decode JSON input value") |
| 316 | msg = f"Unable to decode input argument '{arg_name}'" |
| 317 | extra = { |
| 318 | "json_value": arg_value, |
| 319 | } |
| 320 | raise InvalidInputError(msg, extra=extra) from e |
| 321 | |
| 322 | if logger.isEnabledFor(logging.DEBUG): |
| 323 | logger.debug( |
| 324 | "invoke => %s", |
| 325 | { |
| 326 | "parent_name": parent_name, |
| 327 | "parent_json": textwrap.shorten(parent_json, 144), |
| 328 | "name": name, |
| 329 | "input_args": textwrap.shorten(repr(inputs), 144), |
| 330 | }, |
no test coverage detected