Models¶
models ¶
Assembly: Fonky
Filename: models.py
Author: Terry D. Eppler
Created: 05-31-2022
Last Modified By: Terry D. Eppler
Last Modified On: 05-01-2025
models.py
Copyright © 2026 Terry Eppler
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
You can contact me at: terryeppler@gmail.com or eppler.terry@epa.gov
Prompt ¶
Bases: BaseModel
Prompt model.
Purpose
Represents a structured prompt bundle used to pass instructions, versioning details, output format hints, and a user question through Fonky workflows. The model provides a typed container for prompt metadata that can be serialized by Pydantic.
Attributes:
| Name | Type | Description |
|---|---|---|
instructions |
Optional[str]
|
Instructions value stored by the model. |
id |
Optional[str]
|
Id value stored by the model. |
version |
Optional[str]
|
Version value stored by the model. |
format |
Optional[str]
|
Format value stored by the model. |
question |
Optional[str]
|
Question value stored by the model. |
Source code in models.py
File ¶
Bases: BaseModel
File model.
Purpose
Represents file metadata returned by provider APIs or managed by Fonky workflows. The model stores identity, lifecycle, size, object type, purpose, and filename fields in a consistent Pydantic structure.
Attributes:
| Name | Type | Description |
|---|---|---|
filename |
Optional[str]
|
Filename value stored by the model. |
bytes |
Optional[int]
|
Bytes value stored by the model. |
created_at |
Optional[int]
|
Created at value stored by the model. |
expires_at |
Optional[int]
|
Expires at value stored by the model. |
id |
Optional[str]
|
Id value stored by the model. |
object |
Optional[str]
|
Object value stored by the model. |
purpose |
Optional[str]
|
Purpose value stored by the model. |
Source code in models.py
Document ¶
Bases: BaseModel
Document model.
Purpose
Represents a compact document-style payload containing summary and description text. The model is useful for normalized outputs where only high-level document metadata is required.
Attributes:
| Name | Type | Description |
|---|---|---|
summary |
Optional[str]
|
Summary value stored by the model. |
description |
Optional[str]
|
Description value stored by the model. |
Source code in models.py
Message ¶
Bases: BaseModel
Message model.
Purpose
Represents a normalized chat or tool message payload. The model stores role, content, message type, and optional structured data for conversational and provider-facing workflows.
Attributes:
| Name | Type | Description |
|---|---|---|
content |
Optional[str]
|
Content value stored by the model. |
role |
Optional[str]
|
Role value stored by the model. |
type |
Optional[str]
|
Type value stored by the model. |
data |
Optional[Dict]
|
Data value stored by the model. |
Source code in models.py
Location ¶
Bases: BaseModel
Location model.
Purpose
Represents a high-level location descriptor for tools that need city, region, country, timezone, or location type information. The model keeps user-location context in a provider- neutral shape.
Attributes:
| Name | Type | Description |
|---|---|---|
type |
Optional[str]
|
Type value stored by the model. |
city |
Optional[str]
|
City value stored by the model. |
country |
Optional[str]
|
Country value stored by the model. |
region |
Optional[str]
|
Region value stored by the model. |
timezone |
Optional[str]
|
Timezone value stored by the model. |
Source code in models.py
GeoCoordinates ¶
Bases: BaseModel
GeoCoordinates model.
Purpose
Represents geographic coordinates and optional timezone metadata for geospatial tools. The model stores latitude, longitude, coordinate type, and timezone in a serializable Pydantic container.
Attributes:
| Name | Type | Description |
|---|---|---|
type |
Optional[str]
|
Type value stored by the model. |
latitude |
Optional[float]
|
Latitude value stored by the model. |
longitude |
Optional[float]
|
Longitude value stored by the model. |
timezone |
Optional[str]
|
Timezone value stored by the model. |
Source code in models.py
Forecast ¶
Bases: BaseModel
Forecast model.
Purpose
Represents a simplified weather forecast response. The model stores forecast type, temperature, precipitation, and sky-condition values for tool outputs or normalized provider responses.
Attributes:
| Name | Type | Description |
|---|---|---|
type |
Optional[str]
|
Type value stored by the model. |
temperature |
Optional[int]
|
Temperature value stored by the model. |
precipitation |
Optional[int]
|
Precipitation value stored by the model. |
sky_conditions |
Optional[str]
|
Sky conditions value stored by the model. |
Source code in models.py
Directions ¶
Bases: BaseModel
Directions model.
Purpose
Represents a simplified route or directions payload. The model stores route data and type metadata for mapping, navigation, or location-aware tool responses.
Attributes:
| Name | Type | Description |
|---|---|---|
type |
Optional[str]
|
Type value stored by the model. |
route |
Optional[Any]
|
Route value stored by the model. |
Source code in models.py
SkyCoordinates ¶
Bases: BaseModel
SkyCoordinates model.
Purpose
Represents astronomical coordinate values used by sky, catalog, and observatory workflows. The model stores declination and right ascension in a typed, serializable structure.
Attributes:
| Name | Type | Description |
|---|---|---|
type |
Optional[str]
|
Type value stored by the model. |
declination |
Optional[float]
|
Declination value stored by the model. |
right_ascension |
Optional[float]
|
Right ascension value stored by the model. |
Source code in models.py
Tool ¶
Bases: BaseModel
Tool model.
Purpose
Represents the shared base descriptor for callable tools. The model stores a tool name, provider-facing type, and short description used by function-calling workflows.
Attributes:
| Name | Type | Description |
|---|---|---|
name |
Optional[str]
|
Name value stored by the model. |
type |
Optional[str]
|
Type value stored by the model. |
description |
Optional[str]
|
Description value stored by the model. |
Source code in models.py
Function ¶
Bases: Tool
Function model.
Purpose
Extends the base tool descriptor with callable parameter schema and strictness metadata. The model represents a function-style tool declaration independent of any single provider.
Attributes:
| Name | Type | Description |
|---|---|---|
parameters |
Optional[Dict[str, Any]]
|
Parameters value stored by the model. |
strict |
Optional[bool]
|
Strict value stored by the model. |
Source code in models.py
FileSearch ¶
Bases: Tool
FileSearch model.
Purpose
Represents configuration for a file-search tool. The model stores vector store identifiers, result limits, and optional filters for retrieval workflows.
Attributes:
| Name | Type | Description |
|---|---|---|
vector_store_ids |
Optional[List[str]]
|
Vector store ids value stored by the model. |
max_num_results |
Optional[int]
|
Max num results value stored by the model. |
filters |
Optional[Dict[str, Any]]
|
Filters value stored by the model. |
Source code in models.py
WebSearch ¶
Bases: Tool
WebSearch model.
Purpose
Represents configuration for a web-search tool. The model stores search context size and optional user-location metadata used by search-capable provider workflows.
Attributes:
| Name | Type | Description |
|---|---|---|
type |
Optional[str]
|
Type value stored by the model. |
search_context_size |
Optional[str]
|
Search context size value stored by the model. |
user_location |
Optional[Any]
|
User location value stored by the model. |
Source code in models.py
ComputerUse ¶
Bases: Tool
ComputerUse model.
Purpose
Represents configuration for a computer-use or UI-automation tool. The model stores display dimensions and execution environment metadata for provider tool declarations.
Attributes:
| Name | Type | Description |
|---|---|---|
type |
Optional[str]
|
Type value stored by the model. |
display_height |
Optional[int]
|
Display height value stored by the model. |
display_width |
Optional[int]
|
Display width value stored by the model. |
environment |
Optional[str]
|
Environment value stored by the model. |
Source code in models.py
ToolDef ¶
Bases: Function
ToolDef model.
Purpose
Represents a provider-neutral tool definition bound to a Python callable or object method. The model stores callable metadata, generated parameter schema, provider conversion helpers, and execution behavior for unified tool dispatch.
Attributes:
| Name | Type | Description |
|---|---|---|
name |
Optional[str]
|
Name value stored by the model. |
type |
Optional[str]
|
Type value stored by the model. |
description |
Optional[str]
|
Description value stored by the model. |
parameters |
Optional[Dict[str, Any]]
|
Parameters value stored by the model. |
strict |
Optional[bool]
|
Strict value stored by the model. |
target |
Optional[Any]
|
Target value stored by the model. |
method |
Optional[str]
|
Method value stored by the model. |
handler |
Optional[Callable[..., Any]]
|
Handler value stored by the model. |
category |
Optional[str]
|
Category value stored by the model. |
source_module |
Optional[str]
|
Source module value stored by the model. |
source_class |
Optional[str]
|
Source class value stored by the model. |
callable_name |
Optional[str]
|
Callable name value stored by the model. |
Source code in models.py
553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 | |
from_callable
classmethod
¶
from_callable(
function: Callable[..., Any],
name: Optional[str] = None,
description: Optional[str] = None,
category: Optional[str] = None,
strict: bool = True,
) -> "ToolDef"
Create tool definition from callable.
Purpose
Creates a provider-neutral tool definition from a standalone Python callable. The method derives the callable name, source module, description, parameter schema, strictness flag, and execution handler needed for later tool dispatch.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
function
|
Callable[..., Any]
|
Callable inspected, wrapped, or converted into a provider-neutral tool schema. |
required |
name
|
Optional[str]
|
Human-readable argument name used in validation error messages. |
None
|
description
|
Optional[str]
|
Optional provider-facing description used instead of the callable docstring. |
None
|
category
|
Optional[str]
|
Optional grouping value retained in tool metadata. |
None
|
strict
|
bool
|
Flag indicating whether provider schema validation should be strict. |
True
|
Returns:
| Type | Description |
|---|---|
'ToolDef'
|
Tool definition that wraps the supplied callable for provider-neutral use. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If a supplied value has an unsupported type. |
Error
|
Raised after the underlying exception is wrapped with module, cause, and method metadata. |
Source code in models.py
from_method
classmethod
¶
from_method(
target: Any,
method: str,
name: Optional[str] = None,
description: Optional[str] = None,
category: Optional[str] = None,
strict: bool = True,
) -> "ToolDef"
Create tool definition from object method.
Purpose
Creates a provider-neutral tool definition from a method on an existing object instance. The method validates that the target member exists and is callable, then stores the target, method name, source class, source module, and generated parameter schema.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
target
|
Any
|
Object instance that owns the method being exposed as a tool. |
required |
method
|
str
|
Name of the target method exposed through the tool definition. |
required |
name
|
Optional[str]
|
Human-readable argument name used in validation error messages. |
None
|
description
|
Optional[str]
|
Optional provider-facing description used instead of the callable docstring. |
None
|
category
|
Optional[str]
|
Optional grouping value retained in tool metadata. |
None
|
strict
|
bool
|
Flag indicating whether provider schema validation should be strict. |
True
|
Returns:
| Type | Description |
|---|---|
'ToolDef'
|
Tool definition that resolves and wraps a named method on the supplied object instance. |
Raises:
| Type | Description |
|---|---|
AttributeError
|
If a requested object member or method does not exist. |
TypeError
|
If a supplied value has an unsupported type. |
Error
|
Raised after the underlying exception is wrapped with module, cause, and method metadata. |
Source code in models.py
resolve_callable ¶
Resolve bound callable.
Purpose
Resolves the executable Python callable represented by the tool definition. The method returns a direct handler when one is stored or retrieves the named method from the stored target object after validating the binding.
Returns:
| Type | Description |
|---|---|
Callable[..., Any]
|
Python callable bound to this tool definition. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If a required value is missing, blank, or outside the supported range. |
AttributeError
|
If a requested object member or method does not exist. |
TypeError
|
If a supplied value has an unsupported type. |
Error
|
Raised after the underlying exception is wrapped with module, cause, and method metadata. |
Source code in models.py
call ¶
Execute bound tool callable.
Purpose
Executes the bound tool callable with keyword arguments and returns a neutral response envelope. The method serializes successful results and converts failures into structured error metadata without exposing provider-specific response objects.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
arguments
|
Optional[Dict[str, Any]]
|
Keyword arguments passed to the resolved callable during tool execution. |
None
|
Returns:
| Type | Description |
|---|---|
Dict[str, Any]
|
Dictionary containing execution status, serialized data, error information, and tool metadata. |
Raises:
| Type | Description |
|---|---|
Error
|
If the implementation wraps a provider, parsing, filesystem, or processing failure in the project error type. |
Source code in models.py
to_dict ¶
Export neutral tool dictionary.
Purpose
Exports the tool definition as a provider-neutral dictionary for inspection, persistence, or application-level routing. The method includes schema fields, source metadata, method binding details, and category information.
Returns:
| Type | Description |
|---|---|
Dict[str, Any]
|
Provider-neutral dictionary representation of this tool definition. |
Raises:
| Type | Description |
|---|---|
Error
|
Raised after the underlying exception is wrapped with module, cause, and method metadata. |
Source code in models.py
to_openai ¶
Export OpenAI tool schema.
Purpose
Builds an OpenAI-compatible function tool declaration from the neutral tool definition. The method supplies a function name, description, parameters object, and strictness flag using safe defaults when optional schema fields are absent.
Returns:
| Type | Description |
|---|---|
Dict[str, Any]
|
OpenAI-compatible function-tool schema for this tool definition. |
Raises:
| Type | Description |
|---|---|
Error
|
Raised after the underlying exception is wrapped with module, cause, and method metadata. |
Source code in models.py
to_grok ¶
Export Grok tool schema.
Purpose
Builds a Grok-compatible function tool declaration using the same schema shape used for OpenAI function tools. The method preserves the neutral tool definition while reusing the shared provider conversion path.
Returns:
| Type | Description |
|---|---|
Dict[str, Any]
|
Grok-compatible function-tool schema for this tool definition. |
Raises:
| Type | Description |
|---|---|
Error
|
Raised after the underlying exception is wrapped with module, cause, and method metadata. |
Source code in models.py
to_gemini ¶
Export Gemini tool schema.
Purpose
Builds a Gemini-compatible function declaration from the neutral tool definition. The method returns the function name, description, and parameters object in the schema shape expected by Gemini tool configuration.
Returns:
| Type | Description |
|---|---|
Dict[str, Any]
|
Gemini-compatible function declaration for this tool definition. |
Raises:
| Type | Description |
|---|---|
Error
|
Raised after the underlying exception is wrapped with module, cause, and method metadata. |
Source code in models.py
Result ¶
Represent the result of an HTTP response.
Purpose
Stores the key fields from a requests.Response object in a small, stable
container used by Fonky fetchers and scrapers. The class preserves the original
response while exposing commonly inspected values such as URL, status code, text,
encoding, and headers for serialization and downstream processing.
Attributes:
| Name | Type | Description |
|---|---|---|
url |
str | None
|
Final URL associated with the response. |
status_code |
int | None
|
HTTP status code returned by the request. |
text |
str | None
|
Response body text. |
encoding |
str | None
|
Response encoding reported by |
headers |
str | None
|
Response headers captured from the response object. |
response |
Response | None
|
Original |
Source code in models.py
937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 | |
has_html
property
¶
Indicate whether response text is available.
Purpose
Reports whether the stored response text is represented as a string. This property provides a small compatibility flag for callers that need to decide whether text extraction or HTML-oriented processing can proceed.
Returns:
| Type | Description |
|---|---|
bool
|
|
to_dict ¶
Convert the result to a dictionary.
Purpose
Produces a plain dictionary representation of the response result for JSON-style serialization, testing, logging, or adapter output. Header values are copied into a new dictionary so downstream callers do not mutate the original response headers.
Returns:
| Type | Description |
|---|---|
Dict[str, Any]
|
Dictionary containing the URL, status code, text, encoding, and copied headers. |
Source code in models.py
throw_if ¶
Validate a required model argument.
Purpose
Validates that a required model or tool-schema argument is present and non-empty before schema generation or tool execution.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Argument name included in the validation error message. |
required |
value
|
Any
|
Candidate value checked for |
required |
Returns:
| Name | Type | Description |
|---|---|---|
None |
None
|
Validation succeeds silently; invalid values raise |
Raises:
| Type | Description |
|---|---|
ValueError
|
If a required value is missing, blank, or outside the supported range. |
Source code in models.py
clean_docstring ¶
Clean callable documentation.
Purpose
Normalizes optional callable documentation into a compact description string for generated tool schemas. The function removes indentation artifacts and returns an empty string when no documentation text is available.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
Optional[str]
|
Runtime value converted into a JSON-safe representation. |
required |
Returns:
| Type | Description |
|---|---|
str
|
Cleaned docstring text suitable for reuse as a provider-facing tool description. |
Raises:
| Type | Description |
|---|---|
Error
|
Raised after the underlying exception is wrapped with module, cause, and method metadata. |
Source code in models.py
python_type_to_json_schema ¶
Convert Python annotation to JSON Schema.
Purpose
Maps Python type annotations to the JSON Schema fragments used by provider tool declarations. The function handles primitive types, containers, optional unions, and unknown annotations with conservative schema defaults.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
annotation
|
Any
|
Python type annotation converted into a JSON Schema fragment. |
required |
Returns:
| Type | Description |
|---|---|
Dict[str, Any]
|
JSON Schema fragment that represents the supplied Python type annotation. |
Raises:
| Type | Description |
|---|---|
Error
|
Raised after the underlying exception is wrapped with module, cause, and method metadata. |
Source code in models.py
build_parameter_schema ¶
Build callable parameter schema.
Purpose
Inspects a Python callable signature and builds a provider-neutral JSON Schema parameters object. The function excludes instance parameters, detects required arguments, preserves default values, and uses type hints when available.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
function
|
Callable[..., Any]
|
Callable inspected, wrapped, or converted into a provider-neutral tool schema. |
required |
Returns:
| Type | Description |
|---|---|
Dict[str, Any]
|
Provider-neutral JSON Schema object describing the callable parameters. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If a supplied value has an unsupported type. |
Error
|
Raised after the underlying exception is wrapped with module, cause, and method metadata. |
Source code in models.py
serialize_value ¶
Serialize runtime value.
Purpose
Converts common runtime objects into JSON-safe values for tool execution responses. The function preserves primitive values, recursively serializes mappings and sequences, and normalizes document, model, and dataframe-like objects when possible.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
Any
|
Runtime value converted into a JSON-safe representation. |
required |
Returns:
| Type | Description |
|---|---|
Any
|
JSON-safe representation of the supplied value. |
Raises:
| Type | Description |
|---|---|
Error
|
Raised after the underlying exception is wrapped with module, cause, and method metadata. |