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.1.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.1-py3-none-any.whl (2.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: func2json-0.1.1.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.1.tar.gz
Algorithm Hash digest
SHA256 e92ac9e6290a5c72b1bed5b80e84669f61c0821ed20c7ea12e1ca44e1f157b14
MD5 5d354f3495008eda1dae4ebd18a608b5
BLAKE2b-256 be30f1157a48ab22434c1f0b681ef502f7e82e328985684451542af749e8f00c

See more details on using hashes here.

File details

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

File metadata

  • Download URL: func2json-0.1.1-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.1-py3-none-any.whl
Algorithm Hash digest
SHA256 a68ebf6444b900cea808c7e46036131eaf02bd2461db8a6e9610f74a51370f2a
MD5 0a3e1c377ce6416b25260aa69ffd3993
BLAKE2b-256 ce20a22ad9a3fb7f5052f5ab9a3bc460ccfae8f4abb6c61990057059c9808681

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