[Errors with Flyte task optionally returning None] I have a simple Flyte task which fetches weather ...
e

Erik Dao

over 2 years ago
[Errors with Flyte task optionally returning None] I have a simple Flyte task which fetches weather data from an API like this
weather_data_op = NamedTuple(
    "WeatherDataOP",
    temperature=float,
    humidity=float,
    wind_speed=float
)

@task(cache=False, limits=Resources(cpu="1", mem="2Gi", ephemeral_storage="2Gi"))
def fetch_weather_data(latitude: float, longitude: float) -> weather_data_op:
    api_key = os.environ["WEATHER_API_KEY"]
    url = f"<http://api.openweathermap.org/data/2.5/weather?lat={latitude}&lon={longitude}&appid={api_key}>"
    res = requests.get(url)
    if not (res.status_code == 200):
        return None
    
    response = res.json()
    return weather_data_op(
        temperature=float(response['main']['temp']),
        humidity=float(response['main']['humidity']),
        wind_speed=float(response['wind']['speed']),
    )
Depending on the input longitude and latitude, the weather API might return None. When I execute this task for the inputs that return in None, Flyte will complain
╭─────────────────────────────────────── Traceback (most recent call last) ───────────────────────────────────────╮
│ in <module>:4                                                                                                   │
│                                                                                                                 │
│ ❱ 4 fetch_weather_data(latitude=1237.0, longitude=-154.0)                                                       │
│                                                                                                                 │
│ /opt/bitnami/jupyterhub-singleuser/.local/lib/python3.8/site-packages/flytekit/core/base_task.py:304 in         │
│ __call__                                                                                                        │
│                                                                                                                 │
│ ❱ 304 │   │   return flyte_entity_call_handler(self, *args, **kwargs)  # type: ignore                           │
│                                                                                                                 │
│ /opt/bitnami/jupyterhub-singleuser/.local/lib/python3.8/site-packages/flytekit/core/promise.py:1114 in          │
│ flyte_entity_call_handler                                                                                       │
│                                                                                                                 │
│ ❱ 1114 │   │   │   result = cast(LocallyExecutable, entity).local_execute(child_ctx, **kwargs)                  │
│                                                                                                                 │
│ /opt/bitnami/jupyterhub-singleuser/.local/lib/python3.8/site-packages/flytekit/core/base_task.py:285 in         │
│ local_execute                                                                                                   │
│                                                                                                                 │
│ ❱ 285 │   │   │   outputs_literal_map = self.sandbox_execute(ctx, input_literal_map)                            │
│                                                                                                                 │
│ /opt/bitnami/jupyterhub-singleuser/.local/lib/python3.8/site-packages/flytekit/core/base_task.py:351 in         │
│ sandbox_execute                                                                                                 │
│                                                                                                                 │
│ ❱ 351 │   │   return self.dispatch_execute(ctx, input_literal_map)                                              │
│                                                                                                                 │
│ /opt/bitnami/jupyterhub-singleuser/.local/lib/python3.8/site-packages/flytekit/core/base_task.py:569 in         │
│ dispatch_execute                                                                                                │
│                                                                                                                 │
│ ❱ 569 │   │   │   │   │   expected_output_names[i]: native_outputs[i] for i, _ in enumerate(na                  │
╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
TypeError: 'NoneType' object is not iterable
So I’m wondering what is a proper way to write a Flyte task that optionally returns None?