Skip to content

Instantly share code, notes, and snippets.

@pathikrit
Last active August 14, 2025 18:47
Show Gist options
  • Select an option

  • Save pathikrit/a8b57f2d20eab69d10133a3afadeef09 to your computer and use it in GitHub Desktop.

Select an option

Save pathikrit/a8b57f2d20eab69d10133a3afadeef09 to your computer and use it in GitHub Desktop.
Python vibe_code annotation
from openai import OpenAI
from pydantic import BaseModel, create_model
import inspect
import typing as t
client = OpenAI()
def vibe_code(model: str = "gpt-4o-mini"):
"""Decorator for the lazy"""
def _decorate(fn):
sig = inspect.signature(fn)
name = fn.__name__
doc = inspect.getdoc(fn) or ""
hints = t.get_type_hints(fn)
ret_type = hints.get("return", t.Any)
if not isinstance(ret_type, type) or not issubclass(ret_type, BaseModel):
class _ReturnModel(BaseModel):
result: ret_type # type: ignore
return_model = _ReturnModel
else:
return_model = ret_type
def wrapper(*args, **kwargs):
bound = sig.bind_partial(*args, **kwargs)
bound.apply_defaults()
params = bound.arguments
resp = client.beta.responses.parse(
model=model,
input=[
{
"role": "system",
"content": (
"Implement this Python function call. "
"Only return valid structured JSON."
),
},
{
"role": "user",
"content": {
"type": "text",
"text": f"""
Function: {name}
Signature: {sig}
Docstring: {doc}
Args: {params}
"""
},
},
],
response_format=return_model,
)
parsed = resp.output_parsed
return parsed.result if hasattr(parsed, "result") else parsed
wrapper.__name__ = name
wrapper.__doc__ = doc
wrapper.__signature__ = sig
return wrapper
return _decorate
# Example Usage
@vibe_code()
def add_two_numbers(a: int, b: int) -> int:
"""Return the sum of a and b."""
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment