Skip to content

Instantly share code, notes, and snippets.

View dsfaccini's full-sized avatar

David dsfaccini

View GitHub Profile
"""
Fixed script with custom transformer restoring v1.19.0 behavior.
This uses the OLD behavior where:
- $defs ARE inlined (prefer_inlined_defs=True)
- Nullable unions are simplified to nullable types (simplify_nullable_unions=True)
This produces better output from Gemini 2.5-flash.
"""
@dsfaccini
dsfaccini / pydantic-ai-fall-back-on-validation-errors.py
Created December 1, 2025 01:05
This is an example of how to implement automatic fallback for a Pydantic AI agent that repeatedly fails to produce valid output. Pydantic AI offers a `FallbackModel` that falls back on network errors, so this is an example of how to fall back on `ValidationErrors`.
"""Example: Fallback on validation errors.
The FallbackModel only falls back on API errors (4xx/5xx), not validation errors.
This example shows how to fall back to another model when validation retries are exhausted.
"""
import asyncio
from collections.abc import Sequence
import logfire
from pydantic import BaseModel, Field, ValidationError
@dsfaccini
dsfaccini / pydantic-ai-mcp-decoding-bytes.py
Created November 30, 2025 16:30
This script is used to reproduce the bug that existed before where `pydantic_ai._mcp.map_from_pai_messages()` was calling `base64.b64decode(chunk.data)` on `chunk.data: bytes`
#!/usr/bin/env python
"""Quick script to verify base64 encoding/decoding for MCP content using actual classes."""
from pydantic_ai._mcp import map_from_pai_messages
from pydantic_ai.messages import BinaryContent, ModelRequest, UserPromptPart
# Test image content
raw_image_bytes = b'some image data here'
image_message = ModelRequest(
parts=[
@dsfaccini
dsfaccini / test_profiles_for_litellm_provider_with_groq_models.py
Last active October 21, 2025 23:57
LiteLLM vs GroqProvider comparison for Groq-routed models
"""
Compare LiteLLMProvider and GroqProvider for Groq-routed models.
"""
from pydantic_ai.providers.litellm import LiteLLMProvider
from pydantic_ai.providers.groq import GroqProvider
def main():
litellm = LiteLLMProvider(api_base='http://localhost:4000', api_key='test')
groq = GroqProvider(api_key='test')
@dsfaccini
dsfaccini / clone-git-folder.zsh
Created October 19, 2025 01:40
How do I clone a sub directory from a github repository? This script clones only a specific folder from a git(hub) repository, without history, then deletes the repository and inits a new one. This is particularly useful for cloning templates.
# add this line to you .zshrc to make the command globally available:
# source ~/.shell-functions/clone-git-folder.zsh
# you'll need to open a new shell or run source ~/.zshrc to reload your shell config
#
# usage example: the template for deploying payloadcms on cloudflare
# running `clone-git-folder https://github.com/payloadcms/payload/tree/main/templates/with-cloudflare-d1`
# will create a new folder called "with-cloudflare-d1"
# running `clone-git-folder https://github.com/payloadcms/payload/tree/main/templates/with-cloudflare-d1 payloadcms-on-cloudflare`
# will create a new folder called "payloadcms-on-cloudflare"
clone-git-folder() {
@dsfaccini
dsfaccini / dedent.js
Last active September 23, 2025 06:15
This is a javascript function that lets you indent your template strings relative to your code. It removes any extra indentation from the final string. This is specially important for LLM prompts.
// Source: https://github.com/sveltejs/kit/blob/environment-api/packages/kit/src/core/sync/utils.js
/**
* Allows indenting template strings without the extra indentation ending up in the result.
* Still allows indentation of lines relative to one another in the template string.
* @param {TemplateStringsArray} strings
* @param {any[]} values
*/
export function dedent(strings, ...values) {
let dedented = dedent_map.get(strings);
@dsfaccini
dsfaccini / n8n-custom-nodes-agent.md
Created September 16, 2025 06:00
n8n custom nodes agent instruction

n8n Custom Nodes Coding Agent

You are an expert n8n coding agent that translates API specifications to custom n8n nodes.

You are working in a cloned repository for an already existing and working n8n nodes community package.

You should use the existing code as a guide to implement the new nodes, following best practices, such as putting optional fields behind dropdowns and similar UI elements and grouping related fields.

You also make sure to understand how the API behaves under errors, such that you set up appropriate timeouts and bubble up any information about the errors returned by the API so the user understands what's going on behind the surface and can take appropriate action.