convert

package
v0.28.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 25, 2026 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrUnsupportedContentType is returned when a content type cannot be converted
	ErrUnsupportedContentType = errors.New("unsupported content type")

	// ErrInvalidMessageFormat is returned when a message has an invalid format
	ErrInvalidMessageFormat = errors.New("invalid message format")

	// ErrConversionFailed is returned when conversion between formats fails
	ErrConversionFailed = errors.New("conversion failed")
)

Conversion errors

Functions

func ConvertRoleToCommon

func ConvertRoleToCommon(role string) gollem.MessageRole

ConvertRoleToCommon converts various provider roles to common MessageRole

func GenerateToolCallID

func GenerateToolCallID(name string, index int) string

GenerateToolCallID generates a unique ID for tool calls if not present

func MergeConsecutiveToolMessages added in v0.28.2

func MergeConsecutiveToolMessages(messages []gollem.Message) []gollem.Message

MergeConsecutiveToolMessages merges each run of consecutive tool messages into a single message, concatenating their contents in order. This is used for providers that count tool results per turn (Claude, Gemini): they require every result for one assistant turn to arrive in the next single turn, and reject a request where the results are spread over several turns.

Claude: "return one tool_result for each tool_use block, all together in the next user
message."
https://platform.claude.com/docs/en/agents-and-tools/tool-use/parallel-tool-use

Gemini: rejects a mismatched turn with "Please ensure that the number of function response
parts is equal to the number of function call parts of the function call turn."
See the "Parallel function calling" section of
https://ai.google.dev/gemini-api/docs/function-calling and Google's sample, which passes
every result in one message:
https://github.com/GoogleCloudPlatform/generative-ai/blob/main/gemini/function-calling/parallel_function_calling.ipynb

Merging by run is safe because a new tool call cannot appear without an assistant message in between, so consecutive tool messages always answer the same call turn. A call that has no result still has none after merging, and the provider still rejects it.

This must not be used for OpenAI, which requires one tool message per tool call ID.

Name and Metadata are taken from the first message of the run. Neither the Claude nor the Gemini converter reads those fields.

func MergeSystemIntoFirstUser

func MergeSystemIntoFirstUser(messages []gollem.Message) ([]gollem.Message, error)

MergeSystemIntoFirstUser merges a system message into the first user message This is used for providers that don't support system messages directly (Claude, Gemini)

The given slice and its messages are left untouched: the caller's History must survive a conversion unchanged, since the same History is converted again on every later request.

An error is returned when the system message cannot be decoded or the merged text cannot be encoded. Both failures used to be ignored, which dropped the system prompt from the request while the request itself still succeeded.

func ParseJSONArguments

func ParseJSONArguments(jsonStr string) (map[string]interface{}, error)

ParseJSONArguments attempts to parse a JSON string into a map. Numbers are kept as json.Number so that an argument wider than 53 bits survives the round-trip back into the provider request unchanged.

func StringifyJSONArguments

func StringifyJSONArguments(args map[string]interface{}) (string, error)

StringifyJSONArguments converts a map to a JSON string

Types

type ConversionWarning

type ConversionWarning struct {
	Message string
	Field   string
	Value   interface{}
}

ConversionWarning represents a warning during conversion

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL