OpenParable
Skip to content
← Back to skills

Code Snippets

Documents
code-snippets

Code Snippets is an Agent Skill for documents published in the MicrosoftDocs/semantic-kernel-docs repository on GitHub: a SKILL.md instruction file an AI agent can load to do this task.

How to reference code from sample repos in Agent Framework docs pages using :::code directives, snippet tags, zone pivots, and highlight attributes.

View on GitHub ↗
QUICK START

How to use this skill

Bring this guide into your coding agent with a prompt tailored to the tool you use.

  1. Open your project in Codex.
  2. Copy and paste the prompt below.
  3. Review files and risks before approving any action.
Prompt to paste
Inspect the Agent Skill at https://github.com/MicrosoftDocs/semantic-kernel-docs/blob/HEAD/.github/skills/code-snippets/SKILL.md and all supporting files. Report provenance, permissions, scripts, dependencies, and security risks. Propose an exact installation into .agents/skills/code-snippets, but wait for my approval before writing files or running scripts.

Copying this prompt does not install or run the skill. Review third-party files before use.

Skill: Code Snippets in Docs

Purpose

This skill describes how to reference code from sample repos in Agent Framework docs pages, eliminating inline code duplication.

:::code Directive Syntax

:::code language="python" source="~/../agent-framework-code/python/samples/01-get-started/01_hello_agent.py" id="create_agent" highlight="1-4":::

Parameters

ParameterRequiredDescription
languageYes"python" or "csharp"
sourceYesSnippet file path using docset-relative syntax (for example ~/... or ~/../<dependent-repo>/...)
idNoMatches a snippet tag in source (# <name> / # </name> for Python, // <name> / // </name> for C#)
rangeNoLine range (e.g. "2-24,26"). Cannot coexist with id
highlightNoLines to highlight, relative to the displayed snippet (not the file)

Source Path Conventions

  • In-repo snippets: ~/agent-framework/<path-to-file>
  • Out-of-repo snippets (dependent repositories): ~/../<path_to_root>/<path-to-file>
  • Agent Framework code samples in this repo: ~/../agent-framework-code/python/samples/<section>/<file>.py

Out-of-repo snippet references

If the code file you want to reference is in a different repository, set up that code repository as a dependent repository in .openpublishing.publish.config.json. The path_to_root you assign acts like a folder name for snippet source paths.

Dependent repositories metadata

The dependent_repositories list is required in .openpublishing.publish.config.json for cross-repository references (CRR):

{
  "dependent_repositories": [
    {
      "path_to_root": "<relative path to repository root>",
      "url": "<referenced repository url>",
      "branch": "<branch name of referenced repository>",
      "branch_mapping": {
        "<source repository branch>": "<referenced repository branch>",
        "<source repository branch>": "<referenced repository branch>"
      }
    },
    {
      "path_to_root": "token",
      "url": "https://github.com/Microsoft/token",
      "branch": "main",
      "branch_mapping": {
        "main": "main",
        "develop": "test"
      }
    }
  ]
}
MetadataMeaningRequired
dependent_repositoriesThe CRR relationship list nameYes
path_to_rootRelative folder path to the repository root (folder can be virtual)Yes
urlURL of the reference repository this repo depends onYes
branchDefault branch of the reference repository for buildsYes
branch_mappingOptional source-branch to reference-branch mapNo

Snippet reference syntax

Use the same :::code syntax for CRR snippets as in-repo snippets. Only the source path changes.

  • Start paths with ~ for docset-root-relative references.
  • Use .. segments to move up directories when needed (for example ../../).
  • Prefer readable paths; deeply nested .. chains are harder to maintain.

Example CRR source path:

~/../xamarin-forms-samples/WebServices/TodoREST/TodoAPI/TodoAPI/Startup.cs

[!NOTE] The dependent repository alias is rooted at repo root, but ~ is rooted at the docset's build_source_folder. In this repo, Agent Framework docs use "build_source_folder": "agent-framework", so dependent repositories are referenced like ~/../agent-framework-code/....

[!IMPORTANT] Updating an external code snippet does not automatically trigger a content build. Trigger a build by changing doc content or starting a build manually.

Snippet Tags in Source Files

Python

# <create_agent>
client = OpenAIResponsesClient(...)
agent = client.as_agent(name="...", instructions="...")
# </create_agent>

C#

// <create_agent>
var agent = await client.CreateAIAgentAsync(...);
// </create_agent>

Rules

  • Tag names use snake_case
  • Tags must be unique within a file
  • Tags cannot overlap or nest
  • Keep snippet regions small and focused (5-20 lines ideal)
  • Include all necessary imports within the snippet OR document them separately

Zone Pivots

Every page showing code for both languages uses zone pivots:

:::zone pivot="programming-language-csharp"
:::code language="csharp" source="~/../agent-framework-code/dotnet/samples/..." id="...":::
:::zone-end

:::zone pivot="programming-language-python"
:::code language="python" source="~/../agent-framework-code/python/samples/..." id="...":::
:::zone-end

When to Apply This Skill

Apply :::code references when:

  1. The sample repo structure is stable (samples merged to main)
  2. The sample file has proper snippet tags
  3. You want to eliminate inline code duplication

Until then, use inline code blocks (python / csharp) as a temporary measure.

Common Snippet IDs

IDPurposeTypical file
create_agentAgent instantiation01_hello_agent.py
run_agentNon-streaming run01_hello_agent.py
run_agent_streamingStreaming run01_hello_agent.py
define_toolTool definition02_add_tools.py
create_agent_with_toolsAgent + tools02_add_tools.py
multi_turnThread-based conversation03_multi_turn.py
context_providerMemory/context setup04_memory.py
create_workflowWorkflow builder05_first_workflow.py

Reference