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

Uploaded Python 3

File details

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

File metadata

  • Download URL: func2json-0.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.tar.gz
Algorithm Hash digest
SHA256 2a9fc484c5a8c78067d202dbb5930431c59c5eb1ce9fe09d76ee9eb321139fcd
MD5 01ba27aad740da260bc7a60144c9d06c
BLAKE2b-256 76cbda5050e4a537a81c1f28f10f0e197249e150a70a9370e1bb717096ef2a58

See more details on using hashes here.

File details

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

File metadata

  • Download URL: func2json-0.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-py3-none-any.whl
Algorithm Hash digest
SHA256 e4776752dda73880130e568df97bff221437cd280f39720a41ab5310d1e2b55a
MD5 af28f24c62785ede0e03046e1235b1be
BLAKE2b-256 d2596b2d8d6b768e8bb83b5a357f5b41a402891c450ae52b4bbac22939fce4b7

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