Skip to main content

Convert Python functions to JSON schemas using Pydantic, Compatible with LLM Function Calling.

Project description

Func2JSON

Description

Func2JSON is a Python library designed to convert Python functions into a standardized JSON format, which is particularly useful for API integration and AI function calling in applications like OpenAI's function calling.

Installation

Install Func2JSON using pip:

pip install func2json

Usage

Import the function and use it as follows:

from func2json import execf

def example_function(param1: str, param2: int) -> str:
    """Example function that takes a string and an integer."""
    pass

json_struct = execf(example_function)
print(json_struct)

Output:

{
  "type": "function",
  "function": {
    "name": "example_function",
    "description": "Example function that takes a string and an integer.",
    "parameters": {
      "type": "object",
      "properties": {
        "param1": {
          "type": "string",
          "description": "param1 description"
        },
        "param2": {
          "type": "integer",
          "description": "param2 description"
        }
      },
      "required": ["param1", "param2"]
    }
  }
}

Features

  • Converts function signature and docstring into JSON.
  • Handles parameter types and descriptions.
  • Supports optional parameters.
  • Gracefully handles functions without annotations or docstrings.

Requirements

  • Python 3.10 or higher
  • Dependencies:
    • pydantic
    • docstring_parser

Contributing

Contributions are welcome!

License

Func2JSON is licensed under the MIT License.

Contact

For questions or feedback, please contact thanabordee.noun@gmail.com.

Limitations

  • Does not support functions with *args or **kwargs.
  • Complex type annotations may be converted to strings.

FAQs

  • Q: Why use Func2JSON?

    • A: To easily convert Python functions into a standardized JSON format for use in APIs and AI applications.
  • Q: Does it support async functions?

    • A: Currently, only synchronous functions are supported.

Change Log

  • 0.1: Initial release

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

func2json-0.1.2.tar.gz (2.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

func2json-0.1.2-py3-none-any.whl (2.0 kB view details)

Uploaded Python 3

File details

Details for the file func2json-0.1.2.tar.gz.

File metadata

  • Download URL: func2json-0.1.2.tar.gz
  • Upload date:
  • Size: 2.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.11.10

File hashes

Hashes for func2json-0.1.2.tar.gz
Algorithm Hash digest
SHA256 784a61de2b13c918ce27e206de9287d0398465fd6be7d0a566ebdcfce738a404
MD5 1e2891180cba6e0b2e5020fde0225bbd
BLAKE2b-256 6489af798967e35bac322e73db9656c3e314696794ffe93068de4bd0c65891fe

See more details on using hashes here.

File details

Details for the file func2json-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: func2json-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 2.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.11.10

File hashes

Hashes for func2json-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 0eb9c775fad5e1dd327b643ffd3dd2e1a3e2502d7f7e75fb81c6331f6f23c7bf
MD5 b33a4b98e3e8e6267937312568a7ab54
BLAKE2b-256 e95741305346bfcce8334d7aa23fb667e00e107e283cc84656ac134c63b40f80

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page