Adopt a “type-safe, clean, DRY” style for readability and maintainability.
schema_param: ResponseFormatParam = {...}).Dict[str, str] for headers) rather than Any.# type: ignore when the mismatch can be fixed by correcting the related type(s).ParamSpec to preserve call signatures.Example (import + typed local + helper pattern):
# module scope
import json
def parse_stream_line(line: bytes) -> str:
if not line or line == b"data: [DONE]":
return ""
if hasattr(line, "decode"):
line = line.decode("utf-8")
if line.startswith("data: "):
line = line[len("data: "):]
return line
def parse_stream(rbody):
for line in rbody:
out = parse_stream_line(line)
if out:
yield out
def some_function() -> "ResponseFormatParam":
schema_param: "ResponseFormatParam" = {"type": "text"}
return schema_param
Applying these consistently will improve clarity (fewer surprises), maintainability (less duplication), and type-checker effectiveness (fewer ignores and safer refactors).
Enter the URL of a public GitHub repository