Lightning-AI/pytorch-lightning · error · Exception
Please, return your outputs as a dictionary. Found {output}
Error message
Please, return your outputs as a dictionary. Found {output} What it means
Inside the spawned server, after deserializing the request body, servable_model.serve_step(**body) must return a dict so outputs can be matched with output serializers by key. If it returns a tensor, list, or tuple, a generic Exception is raised in the request handler.
Source
Thrown at src/lightning/pytorch/serve/servable_module_validator.py:170
# Note: This isn't the original version, but a copy.
servable_model.eval()
@app.get("/ping")
def ping() -> bool:
return True
@app.post("/serve")
async def serve(payload: dict = Body(...)) -> dict[str, Any]:
body = payload["body"]
for key, deserializer in deserializers.items():
body[key] = deserializer(body[key])
with torch.no_grad():
output = servable_model.serve_step(**body)
if not isinstance(output, dict):
raise Exception(f"Please, return your outputs as a dictionary. Found {output}")
for key, serializer in serializers.items():
output[key] = serializer(output[key])
return output
run(app, host=host, port=port, log_level="error")
View on GitHub (pinned to 9fed5c27d2)
Solutions
- Change serve_step to return a dict whose keys match configure_serialization's output serializer names, e.g. {"output": result}
- For multiple outputs return {"logits": ..., "probs": ...} with matching serializers
Example fix
# before
def serve_step(self, **kwargs):
return self(kwargs["x"])
// after
def serve_step(self, **kwargs):
return {"output": self(kwargs["x"])} Defensive patterns
Strategy: type-guard
Validate before calling
out = model.serve_step(**model.configure_payload()["body"])
assert isinstance(out, dict), f"serve_step must return a dict, got {type(out)}" Type guard
def returns_dict_serve_step(model, body) -> bool:
with torch.no_grad():
return isinstance(model.serve_step(**body), dict) Prevention
- Make serve_step always return {name: value}
- Keep serializer keys in sync with serve_step output keys via a shared constant
When it happens
Trigger: serve_step returns self(x) (a raw Tensor), a tuple of tensors, or None instead of a dict keyed by output names.
Common situations: Reusing predict_step code that returns a bare tensor; returning multiple outputs as a tuple and expecting positional serialization.
Related errors
- The `configure_payload` method needs to be overridden.
- The `configure_serialization` method needs to be overridden.
- The `serve_step` method needs to be overridden.
- The server didn't start within {self.timeout} seconds.
- Your provided payload {payload} should have a field named "b
AI-assisted analysis of Lightning-AI/pytorch-lightning@9fed5c27d2 (2026-08-28).
Data as JSON: /api/errors/323450cd6beba7c2.
Report an issue: GitHub.