com.rpl.agent-o-rama.langchain4j.json
JSON schema builders for LangChain4j structured outputs and tool specifications.
This namespace provides Clojure-friendly functions for building JSON schemas used throughout LangChain4j for structured outputs, tool parameter definitions, and response formatting. These schemas ensure models return data in predictable formats and enable type-safe tool calling.
any-of
(any-of elems)(any-of description elems)Creates a JSON schema that accepts any of the provided schema types.
This is useful for union types where a field can be one of several different types or values.
Args:
- elems - Collection of JsonSchema objects that are valid alternatives
- description - Optional string description of the schema
Returns:
- JsonAnyOfSchema - Schema that accepts any of the provided types
Example:
;; Field can be either string or number
(lj/any-of "ID can be string or number"
[(lj/string "String identifier")
(lj/number "Numeric identifier")])
;; Field can be any of several enum values
(lj/any-of [(lj/enum "Status" ["active" "inactive"])
(lj/null)])
array
(array item-schema)(array description item-schema)Creates a JSON schema for arrays with a specific item type.
Args:
- item-schema - JsonSchema object defining the type of array elements
- description - Optional string description of the array
Returns:
- JsonArraySchema - Schema for arrays with the specified item type
Example:
;; Array of strings
(lj/array "List of tags" (lc4j/string "A tag"))
;; Array of objects
(lj/array "List of users"
(lj/object {"name" (lc4j/string)
"age" (lc4j/int)}))
boolean
(boolean)(boolean description)Creates a JSON schema for boolean values.
Args:
- description - Optional string description of the boolean field
Returns:
- JsonBooleanSchema - Schema for boolean values
Example:
(lj/boolean "Whether the feature is enabled") (lj/boolean) ; No description
enum
(enum vals)(enum description vals)Creates a JSON schema for enumerated values.
Args:
- vals - Collection of valid string values
- description - Optional string description of the enum
Returns:
- JsonEnumSchema - Schema that accepts only the specified values
Example:
;; Status field with specific values (lj/enum "User status" ["active" "inactive" "pending"]) ;; Priority levels (lj/enum ["low" "medium" "high" "critical"])
from-json-string
(from-json-string json-str)Parses a JSON schema string and constructs a JsonObjectSchema.
Supports JSON Schema features: - Object types with properties, required fields, and additionalProperties - Primitive types: string, integer, number, boolean - Array types with item schemas - Enum types - References using $ref - anyOf combinator for union types - Descriptions for all schema types - Nested objects and arrays
Unsupported features (will throw exceptions): - Constraints: minimum, maximum, minLength, maxLength, pattern, format - Logical combinators: allOf, oneOf, not - Other: const, default
Args: json-str - JSON string containing the schema definition
Returns: JsonSchema object (typically JsonObjectSchema)
Throws: ExceptionInfo if JSON is invalid or contains unsupported features
Example:
(from-json-string
"{\"type\": \"object\",
\"properties\": {
\"name\": {\"type\": \"string\", \"description\": \"User name\"},
\"age\": {\"type\": \"integer\"}
},
\"required\": [\"name\"]}")
int
(int)(int description)Creates a JSON schema for integer values.
Args:
- description - Optional string description of the integer field
Returns:
- JsonIntegerSchema - Schema for integer values
Example:
(lj/int "Number of retries") (lj/int) ; No description
null
(null)Creates a JSON schema for null values.
Returns:
- JsonNullSchema - Schema that only accepts null
Example:
;; Optional field that can be null
(lj/any-of "Optional field"
[(lj/string "String value")
(lj/null)])
number
(number)(number description)Creates a JSON schema for numeric values (integers and floats).
Args:
- description - Optional string description of the number field
Returns:
- JsonNumberSchema - Schema for numeric values
Example:
(lj/number "Price in dollars") (lj/number) ; No description
object
(object name->schema)(object options name->schema)Creates a JSON schema for objects with defined properties.
This is the most commonly used schema type for structured data.
Args:
- name->schema - Map from property names (strings) to their JsonSchema definitions
- options - Optional configuration map or string description:
- :description - String description of the object
- :required - Collection of required property names
- :definitions - Map of reusable schema definitions
- :additional-properties? - Boolean, whether additional properties are allowed
Returns:
- JsonObjectSchema - Schema for objects with the specified properties
Example:
;; Simple object
(lj/object {"name" (lj/string "User name")
"age" (lj/int "User age")})
;; Complex object with options
(lj/object
{:description "User profile with required fields"
:required ["id" "name"]
:additional-properties? false}
{"id" (lj/string "Unique identifier")
"name" (lj/string "Full name")
"email" (lj/string "Email address")
"preferences" (lj/object {"theme" (lj/enum ["light" "dark"])
"notifications" (lj/boolean)})})
;; With string description
(lj/object "Simple user object"
{"id" (lj/string)
"name" (lj/string)})
reference
(reference ref)Creates a JSON schema reference to a definition.
References are used to avoid duplicating schema definitions and enable recursive schemas.
Args:
- ref - String reference path (e.g., “#/$defs/User”)
Returns:
- JsonReferenceSchema - Schema that references another definition
Example:
;; Reference to a definition
(lj/reference "#/$defs/User")
;; Self-reference for recursive structures
(lj/object {"value" (lj/string)
"children" (lj/array (lj/reference "#")})
string
(string)(string description)Creates a JSON schema for string values.
Args:
- description - Optional string description of the string field
Returns:
- JsonStringSchema - Schema for string values
Example:
(lj/string "User's full name") (lj/string) ; No description