Python lib generates its code on-the-fly based on usage

2025-05-121:3624788github.com

Import wisdom, export code. Contribute to cofob/autogenlib development by creating an account on GitHub.

The only library you'll need ever.

Import wisdom, export code.

AutoGenLib is a Python library that automatically generates code on-the-fly using OpenAI's API. When you try to import a module or function that doesn't exist, AutoGenLib creates it for you based on a high-level description of what you need.

  • Dynamic Code Generation: Import modules and functions that don't exist yet
  • Context-Aware: New functions are generated with knowledge of existing code
  • Progressive Enhancement: Add new functionality to existing modules seamlessly
  • No Default Caching: Each import generates fresh code for more varied and creative results
  • Full Codebase Context: LLM can see all previously generated modules for better consistency
  • Caller Code Analysis: The LLM analyzes the actual code that's importing the module to better understand the context and requirements
  • Automatic Exception Handling: All exceptions are sent to LLM to provide clear explanation and fixes for errors.

Or install from source:

git clone https://github.com/cofob/autogenlib.git
cd autogenlib
pip install -e .
  • Python 3.12+
  • OpenAI API key

Set OpenAI API key in OPENAI_API_KEY env variable.

# Import a function that doesn't exist yet - it will be automatically generated
from autogenlib.tokens import generate_token # Use the generated function
token = generate_token(length=32)
print(token)
  1. You initialize AutoGenLib with a description of what you need
  2. When you import a module or function under the autogenlib namespace, the library:
    • Checks if the module/function already exists
    • If not, it analyzes the code that's performing the import to understand the context
    • It sends a request to OpenAI's API with your description and the context
    • The API generates the appropriate code
    • The code is validated and executed
    • The requested module/function becomes available for use
from autogenlib.totp import totp_generator print(totp_generator("SECRETKEY123"))

Add a Verification Function Later

# Later in your application, you need verification:
from autogenlib.totp import verify_totp
result = verify_totp("SECRETKEY123", "123456")
print(f"Verification result: {result}")
# Import a function - AutoGenLib will see how your data is structured
from autogenlib.processor import get_highest_score # Define your data structure
data = [{"user": "Alice", "score": 95}, {"user": "Bob", "score": 82}] # The function will work with your data structure without you having to specify details
print(get_highest_score(data)) # Will correctly extract the highest score
# You can use init function to additionally hint the purpose of your library
from autogenlib import init
init("Cryptographic utility library") # Generate encryption module
from autogenlib.encryption import encrypt_text, decrypt_text
encrypted = encrypt_text("Secret message", "password123")
decrypted = decrypt_text(encrypted, "password123")
print(decrypted) # Generate hashing module
from autogenlib.hashing import hash_password, verify_password
hashed = hash_password("my_secure_password")
is_valid = verify_password("my_secure_password", hashed)
print(f"Password valid: {is_valid}")

Set your OpenAI API key as an environment variable:

export OPENAI_API_KEY="your-api-key-here"
# Optional
export OPENAI_API_BASE_URL="https://openrouter.ai/api/v1" # Use OpenRouter API
export OPENAI_MODEL="openai/gpt-4.1"

Or in your Python code (not recommended for production):

import os
os.environ["OPENAI_API_KEY"] = "your-api-key-here"

By default, AutoGenLib does not cache generated code. This means:

  • Each time you import a module, the LLM generates fresh code
  • You get more varied and often funnier results due to LLM hallucinations
  • The same import might produce different implementations across runs

If you want to enable caching (for consistency or to reduce API calls):

from autogenlib import init
init("Library for data processing", enable_caching=True)

Or toggle caching at runtime:

from autogenlib import init, set_caching
init("Library for data processing") # Later in your code:
set_caching(True) # Enable caching
set_caching(False) # Disable caching

When caching is enabled, generated code is stored in ~/.autogenlib_cache.

  • Requires internet connection to generate new code
  • Depends on OpenAI API availability
  • Generated code quality depends on the clarity of your description
  • Not suitable for production-critical code without review

You can inspect the code that was generated for a module:

from autogenlib.totp import totp_generator
import inspect
print(inspect.getsource(totp_generator))

AutoGenLib creates prompts for the OpenAI API that include:

  1. The description you provided
  2. Any existing code in the module being enhanced
  3. The full context of all previously generated modules
  4. The code that's importing the module/function (new feature!)
  5. The specific function or feature needed

This comprehensive context helps the LLM generate code that's consistent with your existing codebase and fits perfectly with how you intend to use it.

Contributions are not welcome! This is just a fun PoC project.

MIT License

Note: This library is meant for prototyping and experimentation. Always review automatically generated code before using it in production environments.

Note: Of course 100% of the code of this library was generated via LLM


Read the original article

Comments

  • By turbocon 2025-05-152:497 reply

    Wow, what a nightmare of a non-deterministic bug introducing library.

    Super fun idea though, I love the concept. But I’m getting the chills imagining the havoc this could cause

    • By anilakar 2025-05-156:112 reply

      Didn't someone back in the day write a library that let you import an arbitrary Python function from Github by name only? It obviously was meant as a joke, but with AIcolytes everywhere you can't really tell anymore...

      • By __alexs 2025-05-1511:10

        There's one that loads code out of the best matching SO answer automatically https://github.com/drathier/stack-overflow-import

      • By atoav 2025-05-156:412 reply

        Why not go further? Just expose a shell to the internet and let them do the coding work for you /s

        • By dheera 2025-05-1517:021 reply

          It's not really something to be sarcastic about.

          I've actually done this, setting aside a virtual machine specifically for the purpose, trying to move a step towards a full-blown AI agent.

          • By marssaxman 2025-05-1517:21

            Why on earth did you want to do that?

        • By bolognafairy 2025-05-1510:55

          “Twitch does…”

    • By rollcat 2025-05-1510:101 reply

      Flask also started as an April 1st joke, in response to bottle.py but ever so slightly more sane. It gathered so much positive response, that mitsuhiko basically had to make it into a real thing, and later regretted the API choices (like global variables proxying per-request objects).

    • By userbinator 2025-05-152:551 reply

      It's like automatically copy-pasting code from StackOverflow, taken to the next level.

    • By extraduder_ire 2025-05-153:201 reply

      Are there any stable output large language models? Like stablediffusion does for image diffusion models.

      • By tibbar 2025-05-153:322 reply

        If you use a deterministic sampling strategy for the next token (e.g., always output the token with the highest probability) then a traditional LLM should be deterministic on the same hardware/software stack.

        • By extraduder_ire 2025-05-1516:581 reply

          Wouldn't seeding the RNG used to pick the next token be more configurable? How would changing the hardware/other software make a difference to what comes out of the model?

          • By tibbar 2025-05-1517:24

            > Wouldn't seeding the RNG used to pick the next token be more configurable?

            Sure, that would work.

            > How would changing the hardware/other software make a difference to what comes out of the model?

            Floating point arithmetic is not entirely consistent between different GPUs/TPUs/operating systems.

        • By roywiggins 2025-05-155:021 reply

          Deterministic is one thing, but stable to small perturbations in the input is another.

          • By dragonwriter 2025-05-157:291 reply

            > Deterministic is one thing, but stable to small perturbations in the input is another.

            Yes, and the one thing that was asked about was "deterministic" not "stable to small perturbations in the input.

            • By kokada 2025-05-158:391 reply

              This looks "fun" too: commit fixing a small typo -> the app broke.

              • By lvncelot 2025-05-1511:01

                So nothing's changed, then :D

    • By 3abiton 2025-05-155:20

      Sounds like a fun way to learn effective debugging.

    • By emporas 2025-05-154:38

      It imports the bugs as well. No human involvement needed. Automagically.

    • By dheera 2025-05-1517:001 reply

      I mean, we're at the very early stages of code generation.

      Like self-driving cars and human drivers, there will be a point in the future when LLM-generated code is less buggy than human-generated code.

  • By kastden 2025-05-157:341 reply

    You can make it production grade if you combine it with https://github.com/ajalt/fuckitpy

    • By archargelod 2025-05-1510:151 reply

      The repo name made me think it's a tool that stops you from using a project if it detects python:

      "fuck, it's python!" *throws it in the garbage*

  • By selcuka 2025-05-154:341 reply

    This is amazing, yet frightening because I'm sure someone will actually attempt to use it. It's like vibe coding on steroids.

        - Each time you import a module, the LLM generates fresh code
        - You get more varied and often funnier results due to LLM hallucinations
        - The same import might produce different implementations across runs

    • By baq 2025-05-155:223 reply

      There are a few thresholds of usefulness for this. Right now it’s a gimmick. I can see a world in a few years or maybe decades in which we almost never look at the code just like today we almost never look at compiled bytecode or assembly.

      • By latentsea 2025-05-155:492 reply

        There's not much of a world in which we don't check up and verify what humans are doing to some degree periodically. Non-deterministic behavior will never be trusted by default, as it's simply not trustable. As machines become more non-deterministic, we're going to start feeling about them in similar ways we already feel about other such processes.

        • By NitpickLawyer 2025-05-156:071 reply

          > Non-deterministic behavior will never be trusted by default, as it's simply not trustable.

          Never is a long time...

          If you have a task that is easily benchmarkable (i.e. matrix multiplication or algorithm speedup) you can totally "trust" that a system can non-deterministically work the problem until the results are "better" (speed, memory, etc).

          • By Sharlin 2025-05-157:341 reply

            Proving the correctness of the “improvements” is another thing entirely, though.

            • By NitpickLawyer 2025-05-157:55

              I agree. At first the problems that you try to solve need to be verifiable.

              But there's progress on many fronts on this. There's been increased interest in provers (natural language to lean for example). There's also been progress in LLM-as-a-judge on open-ish problems. And it seems that RL can help with extracting step rewards from sparse rewards domains.

        • By jerf 2025-05-1513:24

          You will always get much, much, MUCH better performance from something that looks like assembler code than from having an LLM do everything. So I think the model of "AIs build something that looks recognizably like code" is going to continue indefinitely, and that code is generally going to be more deterministic than an AI will be.

          I'm not saying nothing will change. AIs may be constantly writing their own code for themselves internally in a much more fluid mixed environment, AIs may be writing into AI-specific languages built for their own quirks and preferences that make it harder for humans to follow than when AIs work in relatively human stacks, etc. I'm just saying, the concept of "code" that we could review is definitely going to stick around indefinitely, because the performance gains and reduction in resource usage are always going to be enormous. Even AIs that want to review AI work will want to review the generated and executing code, not the other AIs themselves.

          AIs will always be nondeterministic by their nature (because even if you run them in some deterministic mode, you will not be able to predict their exact results anyhow, which is in practice non-determinism), but non-AI code could conceivably actually get better and more deterministic, depending on how AI software engineering ethos develop.

      • By rollcat 2025-05-1510:052 reply

        There was a story written by (IRRC?) Stanisław Lem: technology went to absurd level of complexity, yet was so important to daily lives that the species' survival depended on it. The knowledge of how everything worked has been long forgotten; the maintainers would occasionally fix something by applying duct tape or prayers.

        Sufficiently advanced technology is indistinguishable from magic.

        We're basically headed in that direction.

      • By Legend2440 2025-05-158:14

        It lets you do things that are simply not possible with traditional programs, like add new features or adapt to new situations at runtime.

        It’s like the strong form of self-modifying code.

HackerNews